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

Larave list routes in the view
how get parameter with POST method from rest client to php
Slim v2 Undefined variable
Pagination only works for blogs using categories.php?
Error in PHP Ajax Code
My pagination links are not working in laravel 5.3 On clicking page number url changes but content remain same
PHP - Remove query string from font URLs
get quantity sold by product last 3 month
PHP array_walk() expects parameter 1 to be array, null given
How to get value from url PHP [duplicate]
Laravel relationships - access the 4th table
Open link only on mobile devices in a new tab
PDF generated with mPDF not working on android
PHP condition check fail
How to retrieve username and password after getting from database to a vriable in laravel 5.2
HY000 2006 MySQL server has gone away

Categories

HOME
flask
list
selenium-builder
scroll
hid
message-queue
out-of-memory
swarm
phpstorm-2017.1
fullcalendar-scheduler
jquery-file-upload
mpu6050
mahout-recommender
sonicwall
weex
amazon-sns
firemonkey-style
fusetools
pubxml
morris.js
basex
adapter
fatfs
private-key
listjs
xmlunit
gnu-classpath
apptentive
testlink
spinner
cordys-opentext
xvfb
text-decorations
cheat-engine
tidal-scheduler
oauth2
python-jira
reverse-dns
spongycastle
cloudwatch
non-linear-regression
cordova-ios
iptv
nivo-slider
apache-modules
mpmovieplayercontroller
ltrace
scheduledexecutorservice
google-identity-toolkit
callouts
apache-pig-grunt
simian
mercurial-extension
univocity
ioctl
webgrind
juniper-network-connect
ruby-on-rails-4.1
search-form
angular-amd
alwayson
explain
componentart
shoes
scala-macros
python-c-extension
tkx
directorysearcher
n-tier-architecture
wicket-1.5
xml-signature
otl
android-2.3-gingerbread
preorder
profiles
cloo
django-settings
facebook-game-groups
illuminate-container
revmob
ember-table
lru
objective-c-runtime
ldif
microformats
reentrancy
2d-vector
icenium
user-forums
tridion-2011
struts2-s2hibernate
microblogging
servlet-container
gaelyk
aptitude
ccnet-config
3270
external-sorting
jquery-effects
database-cloning
webclient.uploaddata

Resources

Encrypt Message