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 - extending Eloquent where clauses depending on dynamic parameters
laravel eloquent hasmany relation returns when one row required
Fatal error: call to undefined function mb_strtolower() in OpenCart [duplicate]
determining ip and port in connection between two client - php
how to make auto increment of string like ALG030001 in php [closed]
Good practice to link script on a page getting loaded by ajax
Connecting to mysql database in php
mysql | Nested set to select box
Laravel - routing to a folder inside “views”
WP mail - Delivery failed: unknown error, how to debug?
Bootstrap modal form replace with JSON
404 Not Found error after commit changes on PHP application Openshift
want to convert my date formate 21/10/60 to 21-10-1960
Difference between sentences of cronjobs
SOLVED - PHP Compare 2 Arrays with time ranges
scandir() scan by increasing number

Categories

HOME
nam
facebook-messenger
android-fragments
workflow
devexpress
braintree
vsts-build
jersey-2.0
twitter-fabric
malware
liferay-7
iptables
apache-kafka-connect
azure-data-lake
tfs2010
roku
tiff
documentum
chromecast
pushdown-automaton
dhcp
mahout-recommender
synthesis
closures
c#-7.0
hidden
codefluent
workday
channel
tracing
ninject
sparkle
basic-authentication
location-services
series
wdk
forms-authentication
strapi
elastica
llvm-ir
stat
right-click
coverage.py
fiware-wirecloud
yoast
return-type
gtk#
google-news
gnome-terminal
popen
deedle
rustdoc
pjax
data-management
nsrunloop
svnserve
cdo.message
wolfram-language
dojo-build
powerpoint-2013
timestamping
as3-api
servicestack-bsd
msgpack
highest
gitlab-omnibus
supercomputers
vdm-sl
green-threads
webproject
image-rotation
global-scope
storing-data
relationships
jquery-mobile-flipswitch
rpg
senchatouch-2.4
fileshare
tabletools
jchartfx
iosched
application-server
mousemotionlistener
stty
jacob
ckeditor.net
onconfigurationchanged
kobold2d
uipangesturerecognizer
autosize
nsfont
dropshadow
uipasteboard
tablet-pc
ninject-interception
ccnet-config
odac
iphone-sdk-4.3
bucket
ning

Resources

Encrypt Message