Pablo
Pablo

Reputation: 6048

How to remove line breaks with PHP or JS

I've tried about everything to delete some extra \n characters in a web application I'm working with. I was hoping someone has encountered this issue before and knows what can be causing this. All my JS and PHP files are UTF-8 encoded with no BOM.

And yes I've tried things like

In JS:

text.replace(/\n/g,"")

In PHP:

preg_replace("[\n]","",$result);
str_replace("\n","",$result);

and when I try

 text.replace(/\n/g,"")

in the firebug console using the same string I get from the server it works but for reason it doesn't work in a JS file.

I'm desperate, picky and this is killing me. Any input is appreciated.

EDIT:

If it helps, I know how to use the replace functions above. I'm able to replace any other string or pattern except \n for some reason.

Answer Explanation:

Some people do and use what works because it just works. If you are like me and for the record I always like to know why what works WORKS!

In my case:

Why this works? str_replace('\n', '', $result)

And this doesn't? str_replace("\n", '', $result)

Looks identical right? Well it seems that when you enclose a string with a character value like \n in double quotes "\n" it's seen as it's character value NOT as a string. On the other hand if you enclose it in single quotes '\n' it's really seen as the string \n. At least that is what i concluded in my 3 hours headache.

If what I concluded is a setup specific issue OR is erroneous please do let me know or edit.

Upvotes: 2

Views: 4559

Answers (4)

alexleonard
alexleonard

Reputation: 1314

Strangely, using

str_replace(array('\r','\n'), '', $string)

didn't work for me. I can't really work out why either.

In my situation I needed to take output from the a WordPress custom meta field, and then I was placing that formatted as HTML in a javascript array for later use as info windows in a Google Maps instance on my site.

If I did the following:

$stockist_address = $stockist_post_custom['stockist_address'][0];
$stockist_address = apply_filters( 'the_content', $stockist_address);
$stockist_sites_html .= str_replace(array('\r','\n'), '', $stockist_address);

This did not give me a string with the html on a single line. This therefore threw an error on Google Maps.

What I needed to do instead was:

$stockist_address = $stockist_post_custom['stockist_address'][0];
$stockist_address = apply_filters( 'the_content', $stockist_address);
$stockist_sites_html .= trim( preg_replace( '/\s+/', ' ', $stockist_address ) );

This worked like a charm for me.

I believe that usage of \s in regular expressions tabs, line breaks and carriage returns.

Upvotes: 0

cbrandolino
cbrandolino

Reputation: 5883

In php, use str_replace(array('\r','\n'), '', $string).

I guess the problem is you also have \r's in your code (carriage returns, also displayed as newlines).

Upvotes: 4

Dan Grossman
Dan Grossman

Reputation: 52372

Both of the PHP functions you tried return the altered string, they do not alter their arguments:

$result = preg_replace("[\n]","",$result);

$result = str_replace("\n","",$result);

Upvotes: 0

user113716
user113716

Reputation: 322462

In javascript, the .replace() method doesn't modify the string. It returns a new modified string, so you need to reference the result.

text = text.replace(/\n/g,"")

Upvotes: 2

Related Questions