Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: HTML String Parsing

by greywolf (Priest)
on Dec 10, 2001 at 00:19 UTC ( [id://130560]=note: print w/replies, xml ) Need Help??


in reply to HTML String Parsing

Since I am not a regex master I would do it in 2 steps. The first simply replaces every newline with a break. The second replaces every pair of breaks with a single break. The & may need to be escaped, I can't test this right now to be sure.
$comment =~ s/\n/&ltbr&gt/g; $comment =~ s/&ltbr&gt&ltbr&gt/&ltbr&gt/g;
It's not the most elegant solution and I'm sure somebody else will whip up something really cool. It should do the trick though.

mr greywolf

Replies are listed 'Best First'.
Re: Re: HTML String Parsing
by thraxil (Prior) on Dec 10, 2001 at 01:41 UTC

    this will break if they put spaces on the blank lines and also probably won't work right if they're on a platform that sends \n\r instead of just \n.

    i usually use something like:

    $comment = "<p>" . (join '</p><p>', grep {!/^\s+$/} split /[\n\r]+/, $comment) . "</p>";

    which could be adapted to use <br> instead of <p> tags if you prefer.

    anders pearson

      Awesome! Thanks! That worked beautifully. I ended up using:

      $comment = (join "\n<BR>", grep {!/^\s+$/} split/[\n\r]+/, $comment);
Re: HTML String Parsing
by Ionizor (Pilgrim) on Dec 10, 2001 at 00:44 UTC

    Hrm... Well I got printed <BR> tags when I tried putting the first part into the script so I set it to <BR> instead of &ltBR&gt and it worked the same as my original, looked neater, and used fewer variables so I kept it (thanks!).

    The second part produces the same results I got with my original script, however...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found