Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Easy way to remove a extra <br> tag with php?

Easy way to remove a extra <br> tag with php?
Thread Tools
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
May 11, 2004, 05:10 AM
 
Howdy, I've got this bug, which I've been spending far too long trying to solve, time to call in the cavalry...

I'm using...

$foo=ereg_replace("<br>",chr(13),$foo);

and....

$foo=ereg_replace(chr(13),"<br>",$foo);

...to register the enter key as a <br> and vica versa when throwing data in and out of a mysql database using an input / edit form.

Problem is that when someone edits everything but the first line, a <br> (which is not seen by that someone) remains.

Anyone know of a quick fix?
     
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
May 11, 2004, 05:24 AM
 
Originally posted by skalie:
Howdy, I've got this bug, which I've been spending far too long trying to solve, time to call in the cavalry...

I'm using...

$foo=ereg_replace("<br>",chr(13),$foo);

and....

$foo=ereg_replace(chr(13),"<br>",$foo);

...to register the enter key as a <br> and vica versa when throwing data in and out of a mysql database using an input / edit form.

Problem is that when someone edits everything but the first line, a <br> (which is not seen by that someone) remains.

Anyone know of a quick fix?
my brain doesn't seem to be functioning properly today, so i'm not sure if i read this right, but, do you need something like this?
ereg_replace("\r\n", "<br />", $foo);
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
skalie  (op)
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
May 11, 2004, 06:15 AM
 
Originally posted by philzilla:
my brain doesn't seem to be functioning properly today, so i'm not sure if i read this right, but, do you need something like this?
ereg_replace("\r\n", "<br />", $foo);
Not quite, well it doesn't seem to do more than the

$foo=ereg_replace(chr(13),"<br>",$foo);

version did.

It's quite a difficult one to describe.....say if I didn't use the ereg_replace the textfield to be editted would appear in the form as

bla<br><br>bla
but after using the ereg_replace the textfield to be editted appears as
bla

bla
removing the second "bla" leaves a <br> after the first "bla" in the code
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
May 11, 2004, 09:09 AM
 
This seems to work fine for me:

[php]
<?php

$str = "a <br><br> b <br><br> c";

print str_replace("<br>", "\r\n", $str);

?>
[/php]

It prints:

Code:
a b c
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 11, 2004, 06:19 PM
 
Do you mean you get unnecessary line spaces at the start or end or your string?

Simple:-

$foo=ereg_replace("<br>",chr(13),trim($foo));

or vice versa... trim() gets rid of linefeeds, tabs and other whitespace from both ends of the string.
Computer thez nohhh...
     
skalie  (op)
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
May 11, 2004, 10:35 PM
 
Originally posted by Simon Mundy:
Do you mean you get unnecessary line spaces at the start or end or your string?

Simple:-

$foo=ereg_replace("<br>",chr(13),trim($foo));

or vice versa... trim() gets rid of linefeeds, tabs and other whitespace from both ends of the string.
Simon it's 5.30 am and you just made my day, thanks a million.

I had tried adding $foo=rtrim($foo); after the ereg_replace statement, but now it's all coming a little clearer.

I knew there had to be an easy method.

Sometimes I think I'm far too much of a perfectionist to be a programmmer, spending hours on tiny potential problems while the real work lags way behind.
     
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
May 12, 2004, 04:56 AM
 
Originally posted by skalie:
spending hours on tiny potential problems while the real work lags way behind.
ugh, tell me about it
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 12, 2004, 09:10 AM
 
Originally posted by philzilla:
ugh, tell me about it
I get distracted with anyone else's problems but my own. Like these forums, for example. Keeps me interested, but sure makes my deadlines stretchy...
Computer thez nohhh...
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:30 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2