tags:

views:

110

answers:

4

I have a variable containing html string. This string has this particular code

<a href="http://www.pheedo.com/click.phdo?s=xxxxxxxx&amp;amp;p=1"&gt;&lt;img border="0" src="http://www.pheedo.com/img.phdo?s=xxxxxxxxxx&amp;amp;p=1" style="border: 0pt none ;" alt=""/></a>

Using regex, how can I remove that. Basically looking for the pheedo.com domain, and stripping out the link and image tag.

Thanks

A: 

This should match the tags (written in PHP):

$regex = "#<a href=\"http:\/\/www\.pheedo\.com[^>]+><img[^>]+><\/a>#"
Nerdling
There is no need to escape quotes or forward slashes in regex. This is always a requirement of the host language, and should not be included without the correct context.
Tomalak
Thanks, this worked perfectly. This worked well with PHP, which I should have stated, is what I was after.
jeremib
+1  A: 

For a more generalized approach, (text/html ads, different urls on the same domain, etc...) you could try

<a.*href="[^"]*pheedo.com[^"]*".*</a>

Just replace any matches you find. Keep in mind that if there is a child <a/>, you're going to have problems.

Stuart Branham
+2  A: 

This is an anti-answer: Don't manipulate arbitrary HTML with regexes! HTML is a really complicated spec, parsing it properly can be a nightmare.

Use a library like phpQuery or the built-in DOMDocument, they know how to deal with all the weirdnesses of HTML for you.

vasi
A: 
    $text = '<a href="http://www.pheedo.com/click.phdo?s=xxxxxxxx&amp;amp;p=1"&gt;&lt;img border="0" src="http://www.pheedo.com/img.phdo?s=xxxxxxxxxx&amp;amp;p=1" style="border: 0pt none ;" alt=""/></a>';
    $reg = "/href=\"(http:\/\/\S+?)\"/i";
    preg_match_all($reg, $text, $matches, PREG_PATTERN_ORDER);

    // $matches[1] should now hold all the domain name "www.pheeedo.com"

I did it this way so you could pass a page to the preg and get all the matching results in an array.../

I did some simular stuff to make this image search tool if you are interested.

http://www.iansimpsonarchitects.com/fraser

You can view the full PHP source from a link on the page.

F.

Fraser