bkiahg has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to substitute \n characters with <.br>\n. This is the code I'm using without the .

$comments =~ s/\n/<.br>\n/;

Any help would be appreciated. Thanks!

Replies are listed 'Best First'.
Re: Substituting Newline Characters
by Joost (Canon) on Mar 15, 2004 at 22:46 UTC
    Yes. $comments =~ s/\n/<.br>\n./g;

    Why you'd want to is beyond me, though.

    edit:
    I see now. You need this $comments =~ s/\n/<br>\n/g;

    Explanation: The g modifier specifies you want to substitute all matches instead of just the first one.

    Ps: you can use <code> tags to type code with worrying about escaping < characters.

    Edit2: I usually find it much easier on the users to do this instead: s/\n\n/<p>\n/g; and leave the single breaks alone.

      (Responding to edit2) Wouldn't that produce something like
      this is a line <p> this is another line <p> this is a third line <p>
      How ugly. Paragraph tags aren't line breaks, they're supposed to enclose a paragraph! At least do s/\n\n/<\/p><p>/g and do something to insert a paragraph tag at the beginning.
Re: Substituting Newline Characters
by tachyon (Chancellor) on Mar 15, 2004 at 23:10 UTC

    You possibly want something like this assuming the goal is to make plain text render with formatting intact as HTML.

    sub escapeHTML { local $_ = shift; # make the required escapes s/&/&amp/g; s/"/&quot;/g; s/</&lt;/g; s/>/&gt;/g; # change tabs to 4 spaces s/\t/ /g; # make the whitespace escapes - not required within <pre> tags s/( {2,})/"&nbsp;" x length $1/eg; # make the brower bugfix escapes; s/\x8b/&#139;/g; s/\x9b/&#155;/g; # change newlines to <br> if desired - not required with <pre> s/\n/<br>\n/g; return $_; }

    cheers

    tachyon

      Compare that to the code from CGI.pm v3.01:

      Cheers Sören

        As it happens i am remarkably familiar with the guts of CGI.pm. I do hope you are not proposing using 5000 lines of CGI.pm for this task? If you are I take it you are aware that outside of a CGI context that it will default to a charset of ISO-8859 aka Latin. You could also note that the /s and /o modifiers on the REs are pointless in context, it does not correctly escape whitespace, and does not deal with \n -> <br> which was at the heart of the original thread.....

        And your point was?

        cheers

        tachyon

      Would you be capable of giving a quick explanation as to what the 'brower bugfix escapes' are? Just curious as I've never seen that used before.

        It is an old Netscape bug. here is one thread on it Google 'x8b browser bug' or similar if this is not enough.

        cheers

        tachyon

Re: Substituting Newline Characters
by wolfi (Scribe) on Mar 16, 2004 at 04:54 UTC

    i read the Q a little different than my other, fellow monks did - so, i'm gonna answer it this way.(Can't hurt. Someone may need the info, regardless.)

    the way i take it, is that you're reading from say - a flatfile (.txt) database - trying to swap out the perl in favour of html, so it can be read by a browser or otherwise converted to html. In other words, you're not exchanging "newlines" but the perl command-like (meta-)characters that represent newlines (\n).

    if i'm not mistaken...
    perl considers the / / in a regex the same as it does double-quotes " " - which means, they're interpolated (carry their full worth - variables are substituted for their values, etc). So, exchanging \n would be exchanging an actual newline - rather than newline characters.

    (i know, it's confusing.)

    anyways...
    if i were exchanging "\n" for "<br>\n", i'd make sure, that the characters were all turned off ("just characters") and not something with more meaning.

    like...

    my $comments =~ s/\\n/\<\.br\>\\n/;

    that extra slash before the special (meta) character tells perl to "turn the special meaning off" (or rather "escape the character" in perl lingo.)

    or, i believe, single-quotes, which don't interpolate - might work as well -

    my $comments =~ s/'\n'/'\n<.br>\n'/;

    (i almost never use that method, so can't be 100% sure, how well it works.)

    in short... if you have a text file, that's got actual tabs, line-breaks, etc in them - try regexes joost and tachyon are suggesting. If you have in the file the actual characters...

    hi\t my name is Count Dracula\n

    use this.

    update...just for the record, i don't think the angle brackets <> need to be escaped. (oops)

      Thanks Guys!

      The g is what I was missing. Although the other code examples were excellent food for thought.

      Thanks again.
Re: Substituting Newline Characters
by neniro (Priest) on Mar 16, 2004 at 16:38 UTC
    If you want to be xhtml-compliant use <br />. Remember to escape the slash in your regex if you use it.

    Update: changed < br/> to <br />.
    Thanks you for the advice, Happy-the-Monk.