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

File request returns error 500
Display Banner after mysql first result
spl_autoload_register couldn't get “use” namespace
How do I retrieve an array value created in a PHP function?
Add space before question mark and exclamation mark
Set timezone according to different country locale wise in php
WordPress: Ajax response overwrites the whole page
PHP file temporarily unavailable
Simplified anarchy “if statement”
swift 3: Get rtf formatted text from MySQL
Doctrine queryBuilder: SQL Injection risk in addOrderBy() method?
Data table error
Checking user privileges before executing functions
SilverStripe sort page list by custom order
Where is this stylesheet being loaded from?
htaccess rewrite URL php parameters

Categories

HOME
qt
extjs
powerbi
libgdx
windows-8.1
dotnetnuke
plpgsql
whmcs
vsts-build
jersey-2.0
implicit-conversion
dji-sdk
position
wagtail
stack-overflow
amazon-emr
multi-step
webstore
uima
fosuserbundle
spam
spring-shell
jest
spyder
yii2-basic-app
data-type-conversion
fusetools
python-appium
openbravo
echosign
tf-idf
sparkle
region
substance
xenapp
windows2012
snap-framework
universal
shapeless
gsp
kendo-listview
polyml
identify
windows-store
hypothesis-test
raytracing
julius-speech
audio-converter
common-table-expression
windows-media-player
roracle
ol3-google-maps
subforms
scala-collections
asp.net-web-api-routing
mongodb-aggregation
test-data
kendo-editor
front-camera
pbx
oracle-fusion-apps
yargs
portal
streamwriter
livereload
wininet
qtcpsocket
groovyfx
ajaxmin
android-syncadapter
noclassdeffounderror
rails-engines
apple-configurator
seadragon
voronoi
wepay
godeps
cfwheels
mixins
espresso
ngcordova
relational-model
ipod
python-curses
mxe
maven-shade-plugin
iodocs
qt5.4
squeezebox
incognito-mode
refit
cidr
xmi
accountmanager
xsd2code
indesign-server
bcdedit
background-thread
image-formats
server-name
platform-independent
uipangesturerecognizer
netstream
litespeed
datacontracts
vows
nsrangeexception
azure-acs
cewolf
tablet-pc
webresponse
jdownloader
aggregator
asdoc
facebook-fbml
tso
html-generation
synthesizer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App