PHP, regex and lazy star

Posted on 2006-03-24
Last Modified: 2012-08-13

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
Question by:AdmiralHorror
    LVL 15

    Accepted Solution

    Try this for removing image tags:

    $string = preg_replace('/<img (.*?)src=(.*?)>/i', "", $string);

    Author Comment

    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"
    LVL 15

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now