asmith Posted August 26, 2008 Share Posted August 26, 2008 Hey guys this string has new lines : $str = "This string has new lines all over it"; when i echo it on the page, the output in the html page is : This string has new lines all over it but if i view the source code of the HTML : it is like when it was in that variable : This string has new lines all over it How can i remove these new lines in the source code too? It means I remove new lines from that variable. I have tried : $str = str_replace("\n","",$str); or even : $str = str_replace(" ","",$str); But no success. The second works fine localy. but do not work online. Any idea? Thanks Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 26, 2008 Share Posted August 26, 2008 Dont know what's the problem of having them in the source code. If you did: <?php echo nl2br($str); ?> you would see those new lines even in the output. I would try the following code and see if it works. <?php str_replace('\r\n', '', $str); ?> Quote Link to comment Share on other sites More sharing options...
asmith Posted August 26, 2008 Author Share Posted August 26, 2008 close one, but didn't work. I want to echo this variable (fetched from database) into the meta description tag, and i don't just feel comfortable having new line for description tag. the nl2br adds <br />. but after str it just removes <br />. but still new lines do not stick together :/ Quote Link to comment Share on other sites More sharing options...
gaza165 Posted August 26, 2008 Share Posted August 26, 2008 What are you planning on the sentence looking like in the source code. If you are using in the meta tag description show me what output you are trying to reach in the source code. G Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 26, 2008 Share Posted August 26, 2008 That's strange. You can try: <?php $replace = array('\r\n', '\n', '\r'); str_replace($replace, '', $str); ?> Or just overwhelm this with: <?php $str = nl2br($str); str_replace('<br />', '', $str); ?> Quote Link to comment Share on other sites More sharing options...
asmith Posted August 26, 2008 Author Share Posted August 26, 2008 @gaza my output now is this : <meta name="description" content="This content goes like this, it as new lines" /> I want it to be : <meta name="description" content="This content goes like this, it as new lines" /> @ GuiltyGear Still no success. I'm using utf8 characters (arabic) . but same is on my local. but local works. but not in online. Quote Link to comment Share on other sites More sharing options...
aschk Posted August 26, 2008 Share Posted August 26, 2008 What you have to realise is that your working environment and online hosted environment are problem running different setups. I suspect (note, this may not be the case) that your development environment is running windows (any variant) and your online hosting is running a linux environment. Thus there is a difference in the way that newlines are written/handled. If i remember correctly windows newline = \r\n linux newline = \n I'll leave you to work out where you're going wrong Quote Link to comment Share on other sites More sharing options...
aschk Posted August 26, 2008 Share Posted August 26, 2008 p.s. str_replace doesn't work on the string as a reference, i.e. to get it to work you need to do $str = str_replace($search, $replace, $subject); NOT just str_replace($search, $replace, $subject); Quote Link to comment Share on other sites More sharing options...
gaza165 Posted August 26, 2008 Share Posted August 26, 2008 I am running on a Linux Server and this code works just fine for me. [color=red]<?[/color] [color=red]$str[/color] = "This string has new lines all over it"; [color=red]$str[/color] = [color=blue]str_replace[/color]("\n", " ", $str); echo [color=red]$str[/color]; [color=red]?>[/color] Let me know if it works G Quote Link to comment Share on other sites More sharing options...
gaza165 Posted August 26, 2008 Share Posted August 26, 2008 sorry forget all the rubbish around it <? $str = "This string has new lines all over it"; $str = str_replace("\n", " ", $str); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted August 26, 2008 Share Posted August 26, 2008 Thank you gaz for your solution, I think you forget to mention that this will ONLY work on a linux setup (or in Zend IDE as i found out recently). There is a better solution, but it uses gaza's template. Here is how: <? $str = "This string has new lines all over it"; $str = str_replace(PHP_EOL, " ", $str); echo $str; ?> Note how the str_replace function uses a PHP constant, in this case PHP_EOL, which will be setup per environment. Thus, a windows server and linux server will have their respective string variants set, making this function platform independent. Quote Link to comment Share on other sites More sharing options...
gaza165 Posted August 26, 2008 Share Posted August 26, 2008 ahh yes, i should of thought of that!!! will remember next time!! Hope that works Gaza Quote Link to comment Share on other sites More sharing options...
asmith Posted August 26, 2008 Author Share Posted August 26, 2008 $str = str_replace(PHP_EOL, " ", $str); worked and I learned a new thing Thanks a TON! Thanks all you guys for all the effort! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.