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

Laravel 5.1 global scopes… Where should I go?
Dropdown in yii2 Not working
Loop Std object array from SOAP to Database PHP
How to keep uploaded file or attachment on server directory contact form 7
PHP Auto-Delete file After Link Delete?
Sessions doesn't pass on within tabs. PHP session
PHP Postmark Invalid argument supplied for foreach() in generated file
Laravel: How to modularize? [duplicate]
Laravel - Get related data with find method (for one record)
PHPExcel trimming leading zero?
Create JSON from mysqli query based on primary key
Having trouble with curl on wamp server
How to get google account email address after oauth2 using the hwioauthBundle
PHP mysql INSERT causing blank first row
Display PHP echo in DIV [closed]
Responsive submenu

Categories

HOME
python-2.7
nam
mc
ant
autocomplete
dronekit-python
portable-class-library
save
onvif
graphql-js
wine
parsley.js
xbox360
bnf
pywinauto
rest-assured
android-5.1.1-lollipop
xsl-fo
bazaar
adobe-illustrator
spring-ldap
el
react-dnd
uilocalnotification
basex
amazon-cloudtrail
cube
fatfs
xenapp
windows2012
universal
seafile-server
microsoft-certifications
android-alarms
best-buy-api
grails-spring-security
newtons-method
secure-coding
nuclio
scala-breeze
photoswipe
ilrepack
entropy
skip-lists
glassfish-4.1
glimpse
word-2016
cefpython
qpixmap
distributed-cache
gulp-uglify
stack-smash
arbre
highest
enaml
compositetype
kefir.js
android-audiomanager
signed
mojo
n-tier-architecture
vs-unit-testing-framework
tabris
angular-ui-select
box2dweb
screwturn
forio-contour
hyperloglog
boinc
stty
tnt4j
lru
ax
ckeditor.net
handshake
gprof
clrprofiler
mmc3
vows
play2-mini
botnet
mediacontroller
rtd
formal-semantics
activestate
odac
resharper-5.0
zend-test
facebook-fbml
ppc
autobench
twitter-feed

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile