preg_replace help

Started on the old forum in October 2012 · view the original in the archive · old links redirect here

8 replies · 603 views · started 14 years ago · 1 min read

6 participants
zain Junior · 43 posts #1

hey guys i am using curl to get source code from a site but i need to know how to use preg_replace to remove this code

<script type="text/javascript">
		    (function() {
		    var em = document.createElement('script'); em.type = 'text/javascript'; em.async = true;
		    em.src = ('https:' == document.location.protocol ? 'https://za-ssl' [^] : 'http://za-cdn' [^]) + '.effectivemeasure.net/em.js';
		    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(em, s);
		    })();
	    </script>

and this last one im also lost with

<a href="http://ad.mo.doubleclick.net/dartproxy/click.handler?destination=http%3A%2F%2Fm.sabc.co.za%2Fsabc%2Fmobile%2Fjsp%2Ftv.jsp&click=http%3A%2F%2Fad-emea.doubleclick.net%2Fclick%3Bh%3Dv8%2F3c6e%2F3%2F0%2F*%2Fr%3B222803474%3B0-0%3B0%3B45967855%3B1-468%2F60%3B35745356%2F35763197%2F1%3B%3B%7Eokv%3D%3Bdcopt%3Dmd%3Bkey1%3Dtop%3Bkey2%3Dbottom%3Bmv1%3D438%3Bmv2%3D203%3Bmv3%3D61%3Bmmanuf%3Dgeneric%3Bmmodel%3Diris+browser%3Bmvideost%3Dfalse%3Bmanigif%3Dtrue%3B%3B%7Esscs%3D%3Fhttp%3A%2F%2F"><img alt="TV Schedules" src="http://s0.2mdn.net/2661318/MID_1258915_L-TS.jpg"></a>

thanks in advance zain

mansi Developer · 60 posts #2

use this.

$d=preg_replace("|<script type=\"text/javscript\"(*.?)</script>|is","",$d);

It will remove all javascript code matching ths sequence.

Hammad Developer · 89 posts #3

Heres code

zain wrote: hey guys i am using curl to get source code from a site but i need to know how to use preg_replace to remove this code

<script type="text/javascript">
		    (function() {
		    var em = document.createElement('script'); em.type = 'text/javascript'; em.async = true;
		    em.src = ('https:' == document.location.protocol ? 'https://za-ssl' [^] : 'http://za-cdn' [^]) + '.effectivemeasure.net/em.js';
		    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(em, s);
		    })();
	    </script>

and this last one im also lost with

<a href="http://ad.mo.doubleclick.net/dartproxy/click.handler?destination=http%3A%2F%2Fm.sabc.co.za%2Fsabc%2Fmobile%2Fjsp%2Ftv.jsp&click=http%3A%2F%2Fad-emea.doubleclick.net%2Fclick%3Bh%3Dv8%2F3c6e%2F3%2F0%2F*%2Fr%3B222803474%3B0-0%3B0%3B45967855%3B1-468%2F60%3B35745356%2F35763197%2F1%3B%3B%7Eokv%3D%3Bdcopt%3Dmd%3Bkey1%3Dtop%3Bkey2%3Dbottom%3Bmv1%3D438%3Bmv2%3D203%3Bmv3%3D61%3Bmmanuf%3Dgeneric%3Bmmodel%3Diris+browser%3Bmvideost%3Dfalse%3Bmanigif%3Dtrue%3B%3B%7Esscs%3D%3Fhttp%3A%2F%2F"><img alt="TV Schedules" src="http://s0.2mdn.net/2661318/MID_1258915_L-TS.jpg"></a>

thanks in advance zain

$hammad = preg_replace('/<script(.*?)<\/script>/is','',$hammad);
$hammad = preg_replace('/<a href="http:\/\/ad(.*?)<\/a>/is','',$hammad);

WapdeviL.iN Most Updated Services Portal.. Want To increase Site Users ???

Ghost Senior · 366 posts #4

mansi wrote: use this. $d=preg_replace("|<script type="text/javscript"(*.?)</script>|is","",$d); It will remove all javascript code matching ths sequence.

you would get problems using this code. as you need to make sure you escape slashes.

Hammad wrote:

$hammad = preg_replace('/<script(.*?)<\/script>/is','',$hammad);
$hammad = preg_replace('/<a href="http:\/\/ad(.*?)<\/a>/is','',$hammad);

i would use this version hammad has offered up. its correct

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
zain Junior · 43 posts #5

thank you guys

Andrew. Newbie · 7 posts #6

Ghost wrote:

mansi wrote: use this. $d=preg_replace("|<script type="text/javscript"(*.?)</script>|is","",$d); It will remove all javascript code matching ths sequence.

you would get problems using this code. as you need to make sure you escape slashes.

You don't need to escape the forward slashes if the delimiter isn't a forward slash. PHP: Delimiters - Manual

arnage Lead · 811 posts #7

Tube is a alternative meta-character so it shouldn't be used as delimiter.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #8

Andrew. wrote: You don't need to escape the forward slashes if the delimiter isn't a forward slash. PHP: Delimiters - Manual

ok your right to some extent. but if you took the time to read the 2 comments left in that link you posted. you'd see its not a wise choice or idea to use meta-characters in the regular expression as a delimiter, thats not saying you cant use them. i was just clearly stating that the option hammad offered up was clearly the better of the 2 choices given to this original poster.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #9

Try to avoid using these:

. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close