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

No filter for the subtotal price in the original code?
How do I loop over this xml structure and create an array from it in PHP
laravel: exception handling
Laravel How do the count for each user
Grabbing the key, closest to a value of an array
makeNthChildOf() method is missing on etrepat/baum laravel package
Data of session delete on session_regenerate_id
Laravel Storage::get() does not return exif data of image
Error in joomla in custom php file
how to trigger a message in header section of webpage after perform an action in wordpress
DateTime function unix timestamp converting wrong
Webhost and Android Studio
Login required in phpdolphin viewing
Unable to access stylesheets in codeigniter
Change total woocomerce order weight
WordPress - request post by url with custom field value

Categories

HOME
firebase
svn
freeradius
functional-programming
android-fragments
plpgsql
braintree
adobe
android-emulator
flurry
navigation
graphql-js
angular2-template
feature-extraction
live-streaming
sonata
jpa-2.0
django-rq
pygobject
user-defined-types
varnish-vcl
cmake-gui
yii2-basic-app
substring
password-generator
window-managers
sap-lumira
ccavenue
php-5.3
intune
location-services
windows2012
memory-fences
snap-framework
epplus
universal
caesar-cipher
seafile-server
busboy
flipkart
raytracing
contactless-smartcard
supertest
knockout-2.0
android-webservice
line-intersection
algebra
word-2016
gapi
ipywidgets
streamwriter
dcount
jtag
groovyfx
node.js-connect
verisign
openmrs
jsr363
android-expansion-files
servicestack-bsd
variable-length-array
hmisc
supersocket.net
modern.ie
physicsjs
wicked-gem
mgwt
baseadapter
nanomsg
sunos
thoughtworks-go
symja
rubber
jquery-mobile-flipswitch
treeline
csslint
forceclose
property-injection
jomsocial
.net-remoting
void-pointers
aero
jquery-dialog
getproperty
word-frequency
liveconnect
acpi
multidrop-bus
sql-parametrized-query
kext
3des
libxslt
formal-semantics
mongrel
pinax
yahoo-maps
dm
ppc
rtti
iphone-sdk-3.1.3
synthesizer

Resources

Encrypt Message