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

What is the meaning of “->” in PHP? [duplicate]
How can i sleep the execution of each iteration of for loop in php
How to store api url in zf2 local.php [closed]
php variable not saving output of query
RewriteRule: bad flag delimiter
Popup Button not working inside Ajax success via JSON
How to execute Jess in php
How to get different Wordpress category links within one php file?
What should an HTML 5 form submit when no value is set [duplicate]
How to present enum table property in Laravel Model
Send request “post” with hidden field and wordpress
PHP and MySQL pagination issue
php - performance of continue state
ZF2 Model getters and setters mismatching form element names
Selecting and matching data from 3 tables
How to get the request data in codeigniter

Categories

HOME
google-chrome
python-2.7
couchdb
enums
swi-prolog
add-on
command-prompt
plpgsql
iptables
flurry
save
phpmqtt
telegraf
tfs2010
cisco
eddystone
lda
driver
rangy
jquery-select2-4
angular4
feature-extraction
freertos
amazon-elb
jqxgrid
spyder
bsd
neo4j.rb
environment
history.js
code-climate
ninject
dnsmasq
audit
es6-modules
express-session
sbjson
utf
icepdf
viewmodel
deployd
ilrepack
sharpdx
redisson
subforms
android-webservice
ptvs
emf-compare
rustdoc
oracle-fusion-apps
svnserve
flipclock
android-expansion-files
grinder
auto-generate
swagger-maven-plugin
phpredis
scala-macros
python-curses
incron
baseadapter
angular-ui-select
siena
rpg
cpu-time
spring-security-acl
ggts
hints
arel
examine
ntlmv2
xmlwriter
simplecov
lynx
telerik-ajax
algol68
navigationcontroller
sigar
kext
fragment-identifier
directshow.net
html-components
rootkit
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App