PHP, regex and lazy star

Hello

I need to write a line of code in PHP to remove image tags from a string.

I am trying the following...

echo eregi_replace("<img .*?>", "", "some string <img src=> with images <img src=   >");

...but keep getting the "Warning: eregi_replace(): REG_BADRPT in .../test.php on line ..." errormessage.

If I remove "?" from the regex it doesn't throw an error, but doesn't work as needed either doing a greedy match.

Please help

Thank you
AdmiralHorrorAsked:
Who is Participating?
 
TomeeboyCommented:
Try this for removing image tags:

$string = preg_replace('/<img (.*?)src=(.*?)>/i', "", $string);
0
 
AdmiralHorrorAuthor Commented:
Ok, that worked, thank you very much.

However, could you please explain to me the meaning of "/" in front of the "<" and the meaning of "/i"
0
 
TomeeboyCommented:
I think that the front slashes just define where the actual pattern starts/stops... though I'm not 100% sure on that :)  I always put them around a regex pattern for any PHP functions that use a regex pattern.  The "i" makes the pattern case insensitive.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.