help removing new line chars – PHP

  php

Q(Question):

Hello,

i have a string that was entered in a web form and stored in a mysql
database where the user entered carriage returns as well as <br>’s
after each line in the form. the data is stored as

line 1<br>
line 2<br>
line 3

i need is to modify the text so instead it reads

line1<br>line2<br>line3

the problem is when i use strpos() and substr() on the multi line
text, i keep getting these invisible \n chars that i can’t seem to get
rid of. even after using trim($string), i still can’t get rid of these
buggers.

is there a function in php that strips the invisible \n chars that are
generated from a web form?

Thanks so much for any suggestions 🙂

Aaron;

A(Answer):

Aaron Collins wrote:

i have a string that was entered in a web form and stored in a mysql
database where the user entered carriage returns as well as <br>’s
after each line in the form. the data is stored as

line 1<br>
line 2<br>
line 3

i need is to modify the text so instead it reads

line1<br>line2<br>line3

the problem is when i use strpos() and substr() on the multi line
text, i keep getting these invisible \n chars that i can’t seem to get
rid of. even after using trim($string), i still can’t get rid of these
buggers.

is there a function in php that strips the invisible \n chars that are
generated from a web form?

IIRC, web forms are supposed to submit line breaks as \r\n so what you can
do to ensure all are removed is something like so:

$text = str_replace("\r", "", $text);
$text = str_replace("\n", "", $text);

or the nasty one liner

$text = str_replace("\r", "", str_replace("\n", "", $text));

You can also do this:

$text = str_replace("\r\n", "", $text);

but if you do have any line breaks that are just "\n" then it won’t get rid
of them.

Note that trim() only trims at the beginning and end of the string, not of
lines in the string, hence my solution.


Chris Hope
The Electric Toolbox – http://www.electrictoolbox.com/

A(Answer):

Chris Hope wrote:

Aaron Collins wrote:

is there a function in php that strips the invisible \n chars that are
generated from a web form?

IIRC, web forms are supposed to submit line breaks as \r\n so what you can
do to ensure all are removed is something like so:

$text = str_replace("\r", "", $text);
$text = str_replace("\n", "", $text);

or the nasty one liner

$text = str_replace("\r", "", str_replace("\n", "", $text));

or the elegant one liner 🙂

$text = str_replace(array("\r", "\n"), "", $text);


USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :

A(Answer):

Pedro Graca wrote:

Chris Hope wrote:

Aaron Collins wrote:

is there a function in php that strips the invisible \n chars that are
generated from a web form?

IIRC, web forms are supposed to submit line breaks as \r\n so what you
can do to ensure all are removed is something like so:

$text = str_replace("\r", "", $text);
$text = str_replace("\n", "", $text);

or the nasty one liner

$text = str_replace("\r", "", str_replace("\n", "", $text));

or the elegant one liner 🙂

$text = str_replace(array("\r", "\n"), "", $text);

Oh cool, I didn’t know you could do that until you pointed it out. I guess
that’s what I get for using PHP for such a long time. According to the
manual the ability to pass arrays was added in 4.0.5


Chris Hope
The Electric Toolbox – http://www.electrictoolbox.com/

A(Answer):

Chris Hope <ch***@electrictoolbox.com> wrote in message news:<10*************@news.athenanews.com>…

Aaron Collins wrote:

i have a string that was entered in a web form and stored in a mysql
database where the user entered carriage returns as well as <br>’s
after each line in the form. the data is stored as

line 1<br>
line 2<br>
line 3

i need is to modify the text so instead it reads

line1<br>line2<br>line3

the problem is when i use strpos() and substr() on the multi line
text, i keep getting these invisible \n chars that i can’t seem to get
rid of. even after using trim($string), i still can’t get rid of these
buggers.

is there a function in php that strips the invisible \n chars that are
generated from a web form?

IIRC, web forms are supposed to submit line breaks as \r\n so what you can
do to ensure all are removed is something like so:

$text = str_replace("\r", "", $text);
$text = str_replace("\n", "", $text);

or the nasty one liner

$text = str_replace("\r", "", str_replace("\n", "", $text));

You can also do this:

$text = str_replace("\r\n", "", $text);

but if you do have any line breaks that are just "\n" then it won’t get rid
of them.

Note that trim() only trims at the beginning and end of the string, not of
lines in the string, hence my solution.

The str_replace() worked!! Thank you so much for your help 🙂 I was
really having fun with those suckers. I didn’t know about the \r.
Thanks for taking the time and helping me out. I really appreciate it.

Aaron;

A(Answer):

aa***@smartwebdevelopment.com (Aaron Collins) wrote in message news:<f1**************************@posting.google. com>…

Hello,

i have a string that was entered in a web form and stored in a mysql
database where the user entered carriage returns as well as <br>’s
after each line in the form. the data is stored as

line 1<br>
line 2<br>
line 3

i need is to modify the text so instead it reads

line1<br>line2<br>line3

<snip>

//strip newlines and leading spaces… logic from Smarty code (but, order reversed)
$strip_search = array(‘%[\r\n]+%m’, // remove CRs and newlines
"![\t ]+$|^[\t ]+!m"); // remove leading/trailing space chars
$strip_replace = array(”,
”);
$page = preg_replace($strip_search, $strip_replace, $page);


| Just another PHP saint |
Email: rrjanbiah-at-Y!com

A(Answer):

Aaron Collins wrote:

Hello,

i have a string that was entered in a web form and stored in a mysql
database where the user entered carriage returns as well as <br>’s
after each line in the form. the data is stored as

line 1<br>
line 2<br>
line 3

i need is to modify the text so instead it reads

line1<br>line2<br>line3

the problem is when i use strpos() and substr() on the multi line
text, i keep getting these invisible \n chars that i can’t seem to get
rid of. even after using trim($string), i still can’t get rid of these
buggers.

is there a function in php that strips the invisible \n chars that are
generated from a web form?

Thanks so much for any suggestions 🙂

Aaron;

The easiest way in my opinion is: strip_tags( $text);

This will strip all tags from the string.

LEAVE A COMMENT