Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Extract and modify IMG SRC tags in an HTML document.

by plaid (Chaplain)
on Apr 27, 2000 at 02:08 UTC ( #9338=note: print w/ replies, xml ) Need Help??


in reply to Extract and modify IMG SRC tags in an HTML document.

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 JAVASCRIPT come to mind. For a nasty one, I'd try something like this:

$html =~ s/(<\s*img\s+.*src\s*=\s*)(")?.*?(?(2)")([\s>])/$1"newimage.j +pg"$3/sig;
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 multiple lines. 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.


Comment on Re: Extract and modify IMG SRC tags in an HTML document.
Download Code
RE: Re: Extract and modify IMG SRC tags in an HTML document.
by Anonymous Monk on Apr 27, 2000 at 11:58 UTC
    What does:
    (?(2)")
    
    do? (I'd dig up the regex book but it's at work.) nice regex, btw. It seems me that the last pair of parentheses are unnecessary. (It might be instructive to use the /x modifier and comment it... this is definitely the most complex regex I have ever tried to understand and comments could make this regex a good reference.) -- anonymous monkey

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://9338]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-09-21 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls