Solved

How do you use PHP to str_replace a line break

Posted on 2004-09-13
8
291 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
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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 …

777 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