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

Parsing JSON file with PHP - status [duplicate]
PHP timezone Etc/GMT+5 becomes -0500?
$form->createView() hangs without error in Symfony2 (runaway memory use)
how to sort object array by key in php [duplicate]
FTP Files are not accessible using fopen or file_exists or file_get_contents
How to call soap api using php?
How to display div depending in CGridVIew in admin in Yii
Facebook PHP applications: restrict access only to some users
Posting video on facebook page?
How to use 'edit' function in responsive calendar (w3widgets)
Creating user friendly URLs with .htaccess file
Passing variable from controller to view not working codeigniter
Prestashop - custom module and display images
Is a combination of credit card's number and expiration date unique? [closed]
Div not expanding with it's child's table
MySQL or SQLITE to store who voted

Categories

HOME
oracle
office365
android-espresso
powerbi
macos-sierra
class
theano
mongoid
malware
repair
u-boot
tweepy
cisco
xamarin-zebble
corona
synthesis
pywinauto
pinterest
gravity
exec-maven-plugin
deepfreeze
unity-container
el
batch-rename
rxjs5
react-dnd
router
python-appium
unrar
msdeploy
sap-lumira
tilemill
xenapp
dotcms
delayed-job
plantuml
pdfminer
android-alarms
aurelia-http-client
system-on-chip
cloudera-sentry
dbcc
asihttprequest
audio-converter
linq-to-excel
.net-micro-framework
ssi
nslayoutconstraint
portability
network-protocols
wildfly-9
java-2d
xcode6.4
tablespace
wicked-pdf
strpos
exists
miniconda
css-paged-media
roxygen2
quercus
nssortdescriptor
mojo
phonegap-facebook-plugin
comctl32
adhoc-polymorphism
tabris
cpu-time
django-sites
meteorite
facebook-game-groups
jmenubar
visual-c++-2005
form-for
insertion
semicolon
asp.net-mvc-3-areas
cyrillic
gprof
simplecov
ip-geolocation
struts2-json-plugin
wndproc
uitextfielddelegate
server-name
pcspim
platform-independent
nsfont
infopath-2007
asio
datacontracts
winsxs
qcar-sdk
active-record-query
jdownloader
ppc

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