php


Better way to replace multiple different words at once in PHP


I've been doing a file_get_contents code for a while now, and I'm getting really tired of using my method to replace multiple different words at once. Sometimes the site I'm file_get_contents from, has changed their layout and therefore has to change up in all this mess.
This is the method I'm using:
$results = file_get_contents('https://example.com/');
$filter1 = str_replace('https://example.com', 'index.php', $results);
$filter2 = str_replace('<script', '<!-- OVERWRITTEN ', $filter1);
$filter3 = str_replace('</script>', ' OVERWRITTEN -->', $filter2);
$filter4 = str_replace('src="http://example.com', 'src="', $filter3);
$output = str_replace('<p>Some Text</p>', '<p>Something Else</p>',$filter4);
echo $output;
Is it a better and cleaner way of replacing multiple different words at once than I have done? I'm not sure about extra delay that PHP has to handle with such mess
Yep, you can do it by sending in arrays:
$results = file_get_contents('https://example.com/');
$output = str_replace(
array('https://example.com', '<script', '</script>', 'src="http://example.com', '<p>Some Text</p>'),
array('index.php', '<!-- OVERWRITTEN ', ' OVERWRITTEN -->', 'src="', '<p>Something Else</p>'),
$results
);
echo $output;
For clarity, the replace code is:
$output = str_replace(
array('https://example.com', '<script', '</script>', 'src="http://example.com', '<p>Some Text</p>'),
array('index.php', '<!-- OVERWRITTEN ', ' OVERWRITTEN -->', 'src="', '<p>Something Else</p>'),
$results
);

Related Links

Restrict a Video online to one view per password, stream only, no download, no repeat
cannot use cookies while curling gmail account
foreach only displaying one element
Handling JSON/Array fields in Eloquent [closed]
php array_map does not find external class [duplicate]
file_exists() not working
The image cannot be displayed because it contains errors (Need help!!!)
How to open a Bootstrap modal when a Submit button redirecting to a different page is clicked?
STRTOTIME take date and data from mysql and concatenate them on php
Creating & setting PHP object properties dynamically using $this keyword
Bigcommerce API authentication and debugging
REST service for uploading image along with other text data using slim framework
Using time_to_sec() in laravel eloquent
Google Search with codeigniter
Device COM# is not currently available
Is it possible to start new session without deleting old one [PHP]

Categories

HOME
mod-rewrite
braintree
ios-simulator
autocomplete
google-webmaster-tools
azure-mobile-services
kurento
tfs2010
locationmanager
google-awareness
complexity-theory
fresco
tomcat8
pickle
susy-sass
tfsbuild
codefluent
web-analytics
batch-rename
ini
opentext
cellular-network
spreadsheetgear
django-import-export
objectmapper
delayed-job
gsp
listjs
creation
assertions
android-n
assert
export-to-pdf
moinmoin
code-push
libreoffice-writer
jgrapht
karabiner
openstack-glance
spring.net
mechanicalturk
front-camera
system.net.mail
declarative
groovyfx
simian
urbit
mathnet
smart-tv
blockquote
replicaset
alwayson
componentart
textscan
dbmigrate
mod-auth-openidc
google-hangouts
dmake
windows-rt
visual-c++-2012
ecos
xmi
meteorite
pbkdf2
eclim
application-server
libxml-js
form-for
ax
background-thread
jquery-cycle
onconfigurationchanged
instance-variables
arrow
prefuse
kobold2d
memory-pool
delphi-prism
groovy-console
upshot
bass
mongrel
j2mepolish

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile