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

php fzaninotto Faker - How to Generate Only 5 Digit Zip Code?
How to disable query logging in console while load Doctrine fixtures?
PHP htmlentities or htmlspecialchars
Database help needed i'm stumped
How to get schedule in next two weeks by using Google Calendar api?
Erro: Query was empty
PDO PHP Update Script not working
Single Login Between Opencart and custom website
I'm trying to get pecl_http
Pagination returning the same posts on each page
Count matches in array of arrays PHP
How to get the max ID in a Join Query in Laravel 5 (Integrity constraint violation:)
Data Leak issue with moxie manager
Use alternative titles for image
Select an sql field from html field and show it in another page
How to perform validation when updating a unique field using Http\Request in Laravel

Categories

HOME
mod-rewrite
memory
opencart
key
add-on
elk-stack
material-components
ontology
share
position
joomla3.2
native-base
static-analysis
yahoo
trace32
parceler
spring-ldap
dashdb
bosun
kaggle
backendless
raima
cellular-network
adapter
fusion
social-networking
docx4j
vcenter
series
wampsharp
android-collapsingtoolbar
cloudera-manager
spring-rabbitmq
char-pointer
amazon-kms
pdfnet
ftp-server
cordys-opentext
ms-dos
xll
google-now
opal-framework
optionaldataexception
xcode8.2
ipp-protocol
webalizer
django-static-precompiler
roracle
spring.net
clrs
polymaps
pecl
mpmovieplayercontroller
front-camera
instafeedjs
msg
carmen
apache-pig-grunt
urbit
mercurial-extension
onepage-checkout
enunciate
outlook.com
fantom
actionpack
kotlin-android-extensions
google-hangouts
cout
xhprof
.htpasswd
nosql-aggregation
hyphen
ggts
unicoins
eager-loading
jsplitpane
clrprofiler
viewpagerindicator
kaazing
jdom
uploading
acpi
mvcrecaptcha
differentiation
kext
rtd
dip
tessellation
odac
fuzzy-comparison
rtti
database-cloning
stretchblt
rootkit

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App