Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP preg_replace making regex optional?

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);
like image 228
slik Avatar asked Sep 17 '26 21:09

slik


2 Answers

  • 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.

like image 72
Mark Byers Avatar answered Sep 19 '26 11:09

Mark Byers


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)

like image 38
Doktor J Avatar answered Sep 19 '26 10:09

Doktor J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!