[SOLVED]Replace Carriage Returns?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

[SOLVED]Replace Carriage Returns?

Post by webcan »

Hello:

I have a variable, $text, that contains some carriage returns:

Line1
Line2
Line3
etc...

What do I do to replace the carriage returns in $text with something that can be sent in the QUERY_STRING? (eg. %20 for space, I dont know what it is for carriage return).

I tried doing it with str_replace, but this doesnt seem to replace anything:

$text = str_replace('\n', '%something', $text);

Thanks,
Peter.
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

OK, I did this:

$text = htmlentities($text);

And nothing happens - the $text variable is unchanged.

Why isn't this working?
And I'd like to figure out why the str_replace above didn't work either.

Thanks,
Peter.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

If you're working under Windows linebreaks are usually "\r\n" hence you need to replace that. Under Unix linebreaks are a simple "\n".

Modify your str_replace there and it should work.
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

Thank you!

That solved my str_replace problem.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

:)
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post by Ixplodestuff8 »

Make sure to use both \r\n and \n, not just one or the other so it works no matter what system you're using.
Post Reply