Forum Moderators: coopster
Or, in other words, is there any way to search backwards using grep?
Thank you for any advice.
[edited by: Josefu at 6:25 pm (utc) on Feb. 20, 2007]
Is this code exactly the same on each page, or it can differ?
If it's the same, then I would preg_replace the whole code with null, otherwise it's hard to do what you require
if it's sth like:
$file = '<div .......>Ad</div>';
then you could
$ad = 'Ad';
while(($ad_pos = strpos($ad))!== false){
$begin = strrpos($file, '<', $ad_pos);
$end = strpos($file, '>', $ad_pos);
//cut it out
}
Hope this helps.
I don't know how to do that with preg_match or preg_replace
Michal
I think what I'll have to do is first check the page for the presence of "$ad" (with a quickie strpos), and if it is there, go through the page's <div> tags one by one - using a "preg_match_all' - to see which one the ad is in - then strip the offending one. But what if the ad appears in a table, or a p tag? Onerous.
Another possiblility would be to strip the offending ad, then do a search and replace for empty tags. This should grab all occasions... but what of empty "clear" div's used to expand content? Again onerous.
I think there is a way to do a "negative lookback" using perl, but I'm not sure that this is recognised by php. It is also a very slow process.
I'll let you know how it works.
a) parses the page for the offending target, and if present:
b) sets up an array of tags that may be around the target (<div>, <p>, etc)
c) does a preg_match_all using first tag in above array
d) walks through the first part of the preg_match_all searching for the offending target and if it is found, puts the entire "between tag" content into a variable, else runs the preg_match_all again with the next tag in the tag array
e) with the above variable in the entire page, does a "search and replace with neant"
Voila. But really. Wouldn't a "find "this" and the "that" just before it" function be useful? I would think so.
[edited by: Josefu at 8:57 pm (utc) on Feb. 21, 2007]