A couple things about turnstep's answer.. on a minor issue,
it will only catch capital SRC, which might not catch them
all. A more important point, though, is that IMG tags
aren't the only ones with SRC attributes.. FRAME and
$html =~ s/(<\s*img\s+.*src\s*=\s*)(")?.*?(?(2)")([\s>])/$1"newimage.j
To go through this in parts.. The first group of
parentheses is catching the beginning of the tag, with
optional whitespace checking, followed by a bunch of junk
(the src attribute doesn't necessarily have to follow the
img, e.g. <img border=0 src="img.gif">). This
matches up to the src= part.
Next, a quote is matched if there is one, and if there is
a quote, the match is taken up to the closing quote.
The match ends with either whitespace or a
tag close. The $1 match is everything up to the name of
the image, which is being preserved. Then, your new image
is subbed in, and the original image name is disregarded.
The i flag is needed to catch src and SRC (and sRc, etc.),
and the s flag in case the image tag is broken up on to
This is a pretty difficult regular expression (which went
through moderate testing..), but if
you're up to reading through the perlre man pages, you
should be able to understand it all. Let me know if there
are any questions about it.
Are you posting in the right place? Check out Where do I post X? to know for sure.
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
Want more info? How to link or
or How to display code and escape characters
are good places to start.