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

How to display and manipulate visual objects in a web app?
Please help me with my comment sections sql query
How to extract data from this JSON ? [closed]
Image cropping and thumb creation
How to draw a name in a database [duplicate]
PHP: Generating category/subcategory UL list from array?
wikipedia servers gzip content
How to escape ' in setParameter
onclick submit check php
php: how to shoow session variable on every php page [duplicate]
Multilingual site: switching languages
jQuery hide() flicker
Cascading dropdown with jQuery
Regex to match alphanumeric characters, underscore, periods and dash, allowing dot and dash only in the middle
Setting Facebook PHP SDK access_token with setAccessToken()
How do I modify the path used by exec in php

Categories

HOME
memory
generics
opencart
mpdf
yaml
activex
activemq
message-queue
dry
ews
braintree
odata
iverilog
tcp
iptables
drag-and-drop
repair
mithril.js
minecraft
angular2-template
static-analysis
telegraf
siesta
sap-fiori
asp.net-core-1.0
pinterest
android-5.1.1-lollipop
boomi
codefluent
scheduled-tasks
bsd
kaggle
ratio
managed-c++
webviewclient
hevc
history.js
basic-authentication
mapnik
argv
plantuml
knights-tour
sca
bean-validation
db2-luw
des
installanywhere
eclipse-gmf
assemblies
windows-store
pyttsx
sendinput
wysihtml5
non-linear-regression
head.js
aurelia-fetch-client
nslayoutconstraint
portability
dex
alertify
mpmovieplayercontroller
deviare
bigdecimal
decoder
nvda
ftp-client
oci
wicked-pdf
management-studio-express
vim-plugin
enunciate
arbre
fanotify
extend
modern.ie
swagger-maven-plugin
google-gdk
innerhtml
android-audiomanager
sql-processor
adhoc-polymorphism
senchatouch-2.4
gpu-programming
typesafe-stack
discovery
flv
session-0-isolation
gitx
audiotoolbox
border-box
system-information
gamesalad
trialware
zend-server-ce
asio
sql-parametrized-query
digiflow
git-log
jdownloader
odac
ixmldomdocument
fgetc
eai
ntruencrypt
sqlsitemapprovider

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