Page 1 of 1

Problem with newline/linefeed in string variable!!!

Posted: Mon Jan 29, 2007 5:33 am
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!

Posted: Mon Jan 29, 2007 6:54 am
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?

Posted: Mon Jan 29, 2007 7:09 am
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)

Posted: Mon Jan 29, 2007 7:30 am
by kaszu
Why use regular expresions, when you can do it without them.

Code: Select all

$string = str_replace(Array("\n", "\r"), '', $string);

Posted: Mon Jan 29, 2007 7:50 am
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!

Posted: Mon Jan 29, 2007 8:19 am
by dude81
ereg_replace doesnt work as I mentioned earlier, kindly ignore.

Posted: Mon Jan 29, 2007 8:39 am
by feyd
Take a look at addcslashes() and its user comments.