CR LF to <br> in REGEX 
Author Message
 CR LF to <br> in REGEX

Hi there,

I'm just off to the dentist and wondered If anyone could help stop the pain
before I get back!

$text=~ s/CR LF/<br>/g;

I want to display text from a Mysql Text field in a table, but I am loosing
the CR LFs. Can anyone help with the above REGEX. I'm new to all this. I've
got this far with my book!! I'm tryingto replace CR
LF with <br>. Is this correct?

Thanks in advance, and wish me luck at the dentist.



Wed, 18 Jun 1902 08:00:00 GMT  
 CR LF to <br> in REGEX

Quote:
> $text=~ s/CR LF/<br>/g;

> I want to display text from a Mysql Text field in a table, but I am
loosing
> the CR LFs. Can anyone help with the above REGEX. I'm new to all this.
I've
> got this far with my book!! I'm tryingto replace CR
> LF with <br>. Is this correct?

Try

$text=~s/\n/<br>\n/g;

Same as in C, \n is the newline.

Quote:
> Thanks in advance, and wish me luck at the dentist.

Heh. Hope it doesn't hurt too much.

Geoff



Wed, 18 Jun 1902 08:00:00 GMT  
 CR LF to <br> in REGEX
Think I've found the answer for myself, but correct me if I'm wrong, ie to
remove carriage returns and insert <br> instead.

$string =~ s/\n/<br>/g;

or


$newstr = ($newstr.$line."<br>");

Quote:
}



Wed, 18 Jun 1902 08:00:00 GMT  
 CR LF to <br> in REGEX
On Wed, 15 Nov 2000 10:12:49 -0000,

Quote:
> Think I've found the answer for myself, but correct me if I'm wrong, ie to
> remove carriage returns and insert <br> instead.

> $string =~ s/\n/<br>/g;

Well, yes, and no. "\n" is not a carriage return, nor is it a line feed,
nor is it any combination of the two. It is the internal representation
of the newline. On some platforms that's character 012 (0xa or 10) and
on others it's character 015 (0xd or 13), and on yet others it's a
combination of the two ("\015\012"). For most network protocols it's
(officially) also "\015\012", also known as a CRLF sequence.

Now, if you need to replace all literal CRLF sequences in a string with
'<br>',

s/\015\012/<br>/g;

\begin{note}
If you need to read this from a file, and you have to do that in a
platform-independent way, then all you can do is open the file in binary
mode and read it that way. Look for sequences of those two characters,
and replace them. One way to do that is by reading the whole file into a
scalar, and using a regular expression substitution.
\end{note}

If your question was a bit confused, and what you really wanted was
replace all newline characters in a string with <br>, then your solution
works fine.

$newstr = '';

Quote:

> $newstr = ($newstr.$line."<br>");

$newstr .= "$line<br>";

Quote:
> }

Ick. That's ugly :)

If you really think that you can get any benefit out of a solution that
uses split, then also use join:

$newstr = join '<br>', split /\n/, $string;

But the s///g is going to be much faster.

As an aside: You should consider using the -w flag and the strict
pragma. The code you presented didn't look like it was used under those
regimes.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | Freudian slip: when you say one thing
Commercial Dynamics Pty. Ltd.   | but mean your mother.
NSW, Australia                  |



Wed, 18 Jun 1902 08:00:00 GMT  
 CR LF to <br> in REGEX

  JP> Think I've found the answer for myself, but correct me if I'm wrong, ie to
  JP> remove carriage returns and insert <br> instead.

  JP> $string =~ s/\n/<br>/g;

that is a newline or linefeed, not a carriage return.



  JP> $newstr = ($newstr.$line."<br>");
  JP> }

blecch!

if you insist on the loop, then learn the .= operator:

        $newstr .= "$line<br>" ;

better to learn join:

        $newstr = join '<br>', split /\n/, $string ;

and if you need to, append another <br> onto that or pass in a empty
string to join:

        $newstr = join '<br>', split( /\n/, $string ), '' ;

uri

--

SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. <FILE> and CR-LF

2. Converting LF to CR+LF

3. DOS Perl convering LF to CR/LF

4. Reading a file without <CR><LF>

5. Sed or Perl to filter out all <CR><LF> in big file?

6. <<<<<< HELP >>>>>>>>

7. <br>

8. replace a return with <br>

9. newbie: Replace \n with <br>\n

10. HTML::TableExtract w/ <br> in header

11. Replace \n with <br>

12. Question: How to <BR>

 

 
Powered by phpBB® Forum Software