Jump to content

[SOLVED] remove new line in the source code


asmith

Recommended Posts

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

 

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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 :/

Link to comment
Share on other sites

@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.

 

 

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.