views:

47

answers:

2

Hi, I have some content that I am trying to parse...

I have html and with in the html I want to replace all my img tags with a custom string. Now one of the problems I have is that some content that comes in: the image tags is between a anchor.

ex.

<a href="link"><img></a>

The following code I have works...but I want it to be optional so in case no anchor tag is surrounded by a img tag does anyone have any good resource on regex or could help me out?

Thank you!

$content = preg_replace('#<a.*?>'.$image_tag.'</a>#i', "{$image_id}", $content);
+1  A: 
  • A question mark after a token makes that token optional.
  • Use (?...) to group several tokens (without creating a capturing group).
  • Combining these, (?...)? makes the group optional.

So you can do this:

$content = preg_replace('#(?:<a.*?>)?'.$image_tag.'(?:</a>)?#i',
                        "{$image_id}",
                        $content);

You might also want to look at alternative solutions that are not based on regular expressions, such as using an HTML parser.

Mark Byers
A: 

Is there any reason

$content = preg_replace('#'.$image_tag.'#i', "{$image_id}", $content);

wouldn't work? If you don't have the ^ and $ anchors on your regular expression, it should just search for $image_tag anywhere in the data whether or not it's surrounded by the <a> tags. If that doesn't work, maybe try:

$content = preg_replace('#(<a.*?>)?'.$image_tag.'(</a>)?#i', "{$image_id}", $content);

which makes the <a> and </a> tags into subpatterns, followed by the ? modifier (i.e. the subpattern may occur 0 or 1 times)

Doktor J