Solved

How do you use PHP to str_replace a line break

Posted on 2004-09-13
8
287 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
Comment Utility
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
Comment Utility
other way round = str_replace all "<br/>" with "\n"
0
 
LVL 2

Expert Comment

by:ShockwaveRK
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:kroplis
Comment Utility
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
Comment Utility
$strReplacement = "replacement";
$aBreaks= Array("\r\n", '\n', '\r');

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

Expert Comment

by:Daij-Djan
Comment Utility
nl2br is for that kroplis
0
 
LVL 1

Accepted Solution

by:
kroplis earned 125 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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