Problem with newline/linefeed in string variable!!!

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
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Problem with newline/linefeed in string variable!!!

Post by josefv »

Hi all! First of all, I'm no PHP Guru. I need help removing a newline/linefeed artifact or special (invisible) character from a string, but it does not show up in for example Mozilla's source viewer as an escaped \n or \r. The source viewer displays the following:

Code: Select all

area = new HyperTextArea('welcome', '<div style="text-align: center;">
Welcome to Abrem Guest House</div>', 420, 200,'');
which wraps over two lines. The linefeed sits between center;"> and Welcome, but i don't know how to remove it!
It is generated from the script:

Code: Select all

print "
        <script language='JavaScript' type='text/javascript'>
        area = new HyperTextArea('welcome', '".ereg_replace('0x0D','',$welcome)."', 420, 200,'');
        </script>
";
As you can see I've attemted to remove the newline/linefeed with ereg_replace but I am not entirely sure how to get it right. I just need to remove any hidden linefeed characters from the $welcome variable.

Ive tried

Code: Select all

ereg_replace('xxx','',$welcome)
where xxx was \n \r and even 0x0A. But to no avail. This code is confusing my page AND ME and causing unexpected output!

Thanks for the help in advance!
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post by josefv »

I think the above commotion was not necessary, so I'll rephrase my question to be more clear:

How do I remove special invisible newline characters, not indicated by \n or \r from a string?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

I think it should be straight

Code: Select all

ereg_replace('\n','', $string);
or simply as kaszu says

Code: Select all

str_replace('\n','', $string)
Last edited by dude81 on Mon Jan 29, 2007 7:34 am, edited 2 times in total.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Why use regular expresions, when you can do it without them.

Code: Select all

$string = str_replace(Array("\n", "\r"), '', $string);
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post by josefv »

Got it! Thanks guys!

Code: Select all

$string = str_replace(Array("\n", "\r"), '', $string);
worked perfectly. Now that my variable is being stripped of all newlines and thingies, my page is no more confused!

Much appreciated!
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

ereg_replace doesnt work as I mentioned earlier, kindly ignore.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Take a look at addcslashes() and its user comments.
Post Reply