Jump to content

Preg_Replace


the-botman

Recommended Posts

heya everyone... ok im trying to replace multiple lines using preg_replace but its just not working please tell me what i am doing wrong here is my code

$page = preg_replace('<script>
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
{
document.getElementById("toolbar_h").innerHTML = "<a href=\"https://addons.mozilla.org/firefox/downloads/file/148244/site_toolbar-initial.rev194-fx.xpi?src=dp-btn-primary\" rel=\"nofollow\" onclick=\"dlreg('','')\"><img src=\"http://site.com/img/toolbar_firefox_small.png\" border=\"0\" /></a>";
}
else
{
document.getElementById("toolbar_h").innerHTML = "<a href=\"http://site.com/crawler/extensions/chrome/mp3se.crx\" rel=\"nofollow\" onclick=\"chrome.webstore.install(); dlreg('',''); return false;\"><img src=\"http://site.com/img/toolbar_chrome_small.png\" border=\"0\" /></a>";
}
</script>','new ad code goes here',$page);

Thanks in advance

Zain

Link to comment
https://forums.phpfreaks.com/topic/268937-preg_replace/
Share on other sites

preg_replace takes a regular expression that is properly formatted with delimiters. Plus, I think you need to escape the forward slashes with backslashes. Kinda hard to verify if the expression is even close without seeing an example of the code to be replaced.

Link to comment
https://forums.phpfreaks.com/topic/268937-preg_replace/#findComment-1382035
Share on other sites

the code im trying to replace is

<script>if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){document.getElementById("toolbar_h").innerHTML = "<a href=\"https://addons.mozilla.org/firefox/downloads/file/148244/site_toolbar-initial.rev194-fx.xpi?src=dp-btn-primary\" rel=\"nofollow\" onclick=\"dlreg('','')\"><img src=\"http://site.com/img/toolbar_firefox_small.png\" border=\"0\" /></a>";}else{document.getElementById("toolbar_h").innerHTML = "<a href=\"http://site.com/crawler/extensions/chrome/mp3se.crx\" rel=\"nofollow\" onclick=\"chrome.webstore.install(); dlreg('',''); return false;\"><img src=\"http://site.com/img/toolbar_chrome_small.png\" border=\"0\" /></a>";}</script>

Link to comment
https://forums.phpfreaks.com/topic/268937-preg_replace/#findComment-1382039
Share on other sites

If you only need the exact string above removed, use str_replace () instead. Regular Expressions should only be used if some of the content is dynamic, and thus cannot be expressed as a static string.

 

As far as escaping forward slashes, that's only needed if you use them as the delimiters. Otherwise they have no special meaning in RegExps.

Link to comment
https://forums.phpfreaks.com/topic/268937-preg_replace/#findComment-1382073
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.