[thelist] howto RegularExpression in PHP

Max Schwanekamp lists at neptunewebworks.com
Mon Jan 8 01:12:52 CST 2007


S. F. Alim wrote:
> I need help with this function `eregi_replace()` in PHP4.4.4. Well actually
> I need help in forming proper regex so I can remove `<img . />` image tag of
> html which is coming from database. All I want is to remove this tag and all
> its attributes.

If you're using regex in PHP, you're better off using the PCRE library 
(preg_match() and friends).  The POSIX Extended regex functions 
(eregi...()) are slower and less useful.

Using preg_replace(), this should do it:

$str = 'text text <img src="evil.gif" /> text text';
echo preg_replace('/<img[^>]*>/iU','',$str);
//outputs text text  text text

A guy named David supplied a rather handy function in the PHP manual 
comments for strip_tags that will strip arbitrary tags.  strip_tags() 
strips all tags _except_ for those specified in the second argument; 
David's strip_selected_tags() strips out only the tags specified, using 
preg_replace.
http://us3.php.net/strip_tags

-- 
Max Schwanekamp
NeptuneWebworks.com
541-255-2171




More information about the thelist mailing list