Solved

How do you use PHP to str_replace a line break

Posted on 2004-09-13
8
288 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
setup wamp server for first time 2 41
Popup to change image and store url in database 2 30
object oriented programming comparison 5 52
<? versus <?php 5 35
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now