...

View Full Version : Regular expression help



nikos101
05-01-2009, 08:42 AM
I'm cleaning up the remains of a virus


How would I remove

<iframe>junk</iframe>

in the following code

<iframe>bad code</iframe><iframe>junk</iframe>

If I use the regex:

<iframe.*bad code.*iframe>

it matches

<iframe>bad code</iframe><iframe>junk</iframe>

not just the

<iframe>junk</iframe> that I wanted.

I hope that makes sense :)

Eran
05-01-2009, 10:31 AM
well your code is make sense
you ask to start with <iframe. in the mid you want "bad code". and in the end you want iframe>.
<iframe>bad code</iframe><iframe>junk</iframe>


you need to get more specific on your regex if you want only bad code.
<iframe>.*e<\/iframe>
i ended it with "e</iframe>"
match <iframe>bad code</iframe>

or <iframe>j.*k<\/iframe>
match <iframe>junk</iframe>

or <iframe>junk<\/iframe>
match <iframe>junk</iframe>


___________
i had this virus too
on the end of the page before the </body>

nikos101
05-01-2009, 10:34 AM
thanks but the bad code can be anything

nikos101
05-01-2009, 10:35 AM
I should be more specific, its the

<iframe>junk</iframe> that I need to match

Eran
05-01-2009, 10:36 AM
what are you tring to do?
you want to delete the bad code or junk?

Eran
05-01-2009, 10:57 AM
junk and bad code could be anything or there are some sort of a string that can ident the "junk"

oesxyl
05-01-2009, 08:23 PM
try this:


<?php

$text = '<iframe>bad code1</iframe><iframe>bad code2</iframe><iframe>bad code3</iframe><iframe>bad code4</iframe><iframe>bad code5</iframe><iframe>bad code6</iframe><iframe>junk</iframe>';

function reg($v){
$v = preg_replace("/\//","\/",$v);
return '/('.$v.')/';
}

preg_match_all("/(<iframe[^<]+<\/iframe>)/",$text,$matches);
$re1 = array_map("reg",$matches[1]);
$re2 = array_fill(0,count($re1),'$1');
array_pop($re2);
array_push($re2,'');

$result = preg_replace($re1,$re2,$text);

print $result;

?>


best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum