How do you use PHP to str_replace a line break

What is a good way to replace a line break using php?
I am using a php web form that will read from an xml document but the data is really stored in a mysql db.

Kag01Asked:
Who is Participating?
 
kroplisConnect With a Mentor Commented:
nl2br() does NOT replace "\r".
otherwise, it wouldn't have spawned as many comments on it:
http://php.grafton.lv/manual/en/function.nl2br.php

for example - $s_data = ereg_replace("(\r\n|\n|\r)", "<br />", $s_data);
0
 
cLFlaVACommented:
Replace it with what?

The nl2br() function will replace the new line character with "<br>", so the new line displays as a new line in HTML.
0
 
Daij-DjanCommented:
other way round = str_replace all "<br/>" with "\n"
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ShockwaveRKCommented:
Depends on what you want to do with the linebreak

if you want to replace it see the answers above

if you just wnat to get rid of use trim

$new_Var = trim ( $line_with_break);

--
greetz,
Robert
The Netherlands.
0
 
kroplisCommented:
yeah, but trim only strips whitespaces from the sides
and also str_replace with "\n" might miss carriage returns - "\r"
i would advice using

$string = str_replace($string, "\r\n", $replacement);
$string = str_replace($string, "\n", $replacement);
$string = str_replace($string, "\r", $replacement);

it makes it more portable, as Mac uses "\r" for newline, *NIX systems use "\n" and windows uses "\r\n" for newline.
0
 
SkonenCommented:
$strReplacement = "replacement";
$aBreaks= Array("\r\n", '\n', '\r');

$string = str_replace($string, $aBreaks, $strReplacement);
0
 
Daij-DjanCommented:
nl2br is for that kroplis
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.