We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

PHP, regex and lazy star

AdmiralHorror
on
Medium Priority
676 Views
Last Modified: 2012-08-13
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
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Try this for removing image tags:

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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"
CERTIFIED EXPERT

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.