Solved

How do you use PHP to str_replace a line break

Posted on 2004-09-13
8
296 Views
Last Modified: 2008-02-26
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.

0
Comment
Question by:Kag01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12043805
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
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 12044203
other way round = str_replace all "<br/>" with "\n"
0
 
LVL 2

Expert Comment

by:ShockwaveRK
ID: 12044873
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Expert Comment

by:kroplis
ID: 12048870
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
 
LVL 4

Expert Comment

by:Skonen
ID: 12051084
$strReplacement = "replacement";
$aBreaks= Array("\r\n", '\n', '\r');

$string = str_replace($string, $aBreaks, $strReplacement);
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 12053821
nl2br is for that kroplis
0
 
LVL 1

Accepted Solution

by:
kroplis earned 125 total points
ID: 12054096
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question