Newline problem "\n"

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm having a problem getting rid of newlines.

say I have this situation:

$cat = "A nice\n cat";

The "\n" is an escape character which creates a newline right there.

Now, how would I get rid of the newline in that string?

How would I then make that string: "A nice cat"  ? without the newline
being there?

Also, another question.

If I have a string, let's say: "The man\nwent to the\tdairy\ryesterday"
Does anybody know of a function or of a way to display or find out the
escaped characters in the string? Something that would point out which
escaped characters were in the string?

The reason that I am asking is because I put a form on a web page and I
want to find out what escaped characters are in the resulting $_POST
variable for one of the fields, for a javascript reason.

Any help is much appreciated.


Re: Newline problem "\n"

mudge wrote:

Quoted text here. Click to load it

$cat = str_replace("\n", "", $cat);
Quoted text here. Click to load it

How about strpos($cat, "\n");

Chris Hope - The Electric Toolbox - /

Re: Newline problem "\n"

I can't beleive it. I figured it out.

You see,   when you fill in a field in a form and it gets past off to a
$_POST in a php script,  both "/n" and "/r" get attatched to each line

The reason I couldn't get rid of the line breaks, was because I was
only replacing "/n" when I also needed to replace "/r" as well.


Re: Newline problem "\n"

Quoted text here. Click to load it

Be careful.  this may depend on what OS platform your client is on.
All three of Linux, Mac, Windows use differing line termination.

If memory serves; \n, \r\n, \n\r (respectively)

If in the HTTP protocol spec it's required for any client to send \r\n
for EOL, then you may disregard my advice.


Quoted text here. Click to load it

Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
                305 321-1144 (mobile /

Site Timeline