PHP preg_replace making regex optional?

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('#(?:)? '.

$image_tag. '(?:)? #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.

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 tags. If that doesn't work, maybe try: $content = preg_replace('#()? '.

$image_tag. '()? #i', "{$image_id}", $content); which makes the and tags into subpatterns, followed by the?

Modifier (i.e. The subpattern may occur 0 or 1 times).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send