Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2489
  • Last Modified:

Need regex: find img tags in a string

I have a generated HTML string where I want to strip out all HTML tags except some special ones, so I am looking for a regular expression that can help me.

So my current idea is:
1) use a string replace to make (example) <img .../> tags to [img.../]
2) use a function like stripHTML to get rid of all HTML tags
3) convert the [img.../] back into <img.../>

So I need a query that finds <img...> and <img ... /> (with and without an "/" at the end).
And the same query for [img...]. The second one needs, as far as I know, some escapes, this is why I am not sure if it can be done easily.
2 Solutions
Fernando SotoCommented:
Can you post a short file/excerpt of a file showing exactly what you want replaced with what.

Seeming not all regular expressions used by a programming language are not all built the same way what programming language are you needing this for, PHP, C# or other?
if you don't have things like
<IMG SRC = "foo.gif" ALT = "A > B">

Fernando SotoCommented:
Not sure what you want, sorry.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

In perl, use a slightly modified version of ozo's code to handle the img tag translation.  I have added the second line to remove all html tags (or anything that looks line one).  My perl is a bit rusty though, so I've added a PHP version below.


$html =~ s/<(img\b.*?)>/[$1]/xsgi;
$html =~ s/<(.*?)>//xsgi;
$html =~ s/\[(img\b.*?)\]/<$1>/xsgi;

// PHP

$html = preg_replace("/<(img .*?)>/","[$1]",$html);
$html = preg_replace("/<(.*?)>/","",$html);
$html = preg_replace("/\[(img .*?)\]/","<$1>",$html);
Scott BennettManager TechnologyCommented:
<[iI][mM][gG][^>]*> will match <img...> and <img.../>
 if you find use that to find all the starting and ending positions of all the img tags in the string you can then replace the starting character with [ and the endpostion caracter with ]
SmoerbleAuthor Commented:
@jonathanmelnick and SBennett:
Thanks, perfect solutions!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now