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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You're relying on a couple of things here, which may be OK and which may not be. If they are, disregard the code, which was written on the fly (but works with the sample input!). First, consider a line such as:

<img src="blern.gif" /><span style="display:block" >foo!</span>

That's going to pass your test. There's also that pesky problem that the tag need not all be on the same line:

<img src="gern.jpg" border="0" style="display:block" />

Pile on top of that the problem that it may already have a style element that doesn't contain the "display:block" and you're looking at trouble.

So you could sanitize your input to make sure none of these is the case, but then you might as well make the whole thing more CSS-friendly (image spacers indeed! =). Actually, better than adding the inline style would be creating a class in your stylesheet that specifies display:block. But I'm getting ahead of myself, and probably of where you want to be.

A more robust solution than any involving a hand-rolled regex would be to write a filter, presumably using HTML::Parser or a subclass thereof, that does the job, along the following lines:

#!/usr/bin/perl # note, warnings are off because this shouldn't generate # 'em except where there is no "style" attribute on # image tags. use strict; use HTML::Parser; my $p=HTML::Parser->new( default_h => [ sub { print $_[0] }, 'text' ], start_h => [\&start_tag, 't +agname, attr, text']); $p->parse_file(*DATA); sub start_tag { my($tagname, $attr, $origtext) = @_; if ( ! ( $tagname eq "img") ) { print $origtext; return; } if ($attr->{style} !~/display:block/) { # avoid messy issues with other styles that may have been defined $attr->{style} = "display:block;". $attr->{style}; } print "<$tagname"; foreach ( keys %$attr) { print qq{ $_="} .$attr->{$_}.qq{"}; } print ">"; } __DATA__ <html> <head> <title>Boo</title> </head> <body bgcolor="#434343"> <h3>Hello</h3> <img src="witches.jpg"> <img src="macbeth.jpg" style="border: thick double #fcfcfc;"> <img src="foofaraw" style="width: 90"> <img src="cauldron.jpg" style="display:block"> <img src="fenny_snake.png"><span style="display: block">Hi there</sp +an> </body> </html>

I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche


In reply to Re: How do I write a regex for 'does not contain' a string. by arturo
in thread How do I write a regex for 'does not contain' a string. by IraTarball

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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> <u> <ul>
  • 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 intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2023-09-27 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?