最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove line breaks with PHP or JS - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited May 2, 2012 at 9:06 Prof. Falken 24.9k20 gold badges103 silver badges179 bronze badges asked Jan 23, 2011 at 2:28 PabloPablo 6,0687 gold badges37 silver badges52 bronze badges 5
  • Are you actually storing the result from text.replace()? It doesn't replace the contents of the existing variable, it makes a new one. – Jeff Hubbard Commented Jan 23, 2011 at 2:30
  • 2 "[Please Read]" ? I don't think that's gonna work. – JakeParis Commented Jan 23, 2011 at 2:31
  • Did you try \u patter modified (i.e. Pattern strings are treated as UTF-8)? php/manual/en/reference.pcre.pattern.modifiers.php – Marcin Commented Jan 23, 2011 at 2:36
  • 1 The line break might be \r\n as well. If you only remove one half, it might still be interpreted by browsers. Maybe even try [\r\n\v]. – mario Commented Jan 23, 2011 at 2:58
  • @mario i came down to the conclusion that at least in JS [\r\n\v] is not working because the '\n' shows up in the browser so is a string and [\r\n\v] looks for the character value. – Pablo Commented Jan 23, 2011 at 3:20
Add a ment  | 

4 Answers 4

Reset to default 4

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

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,"")

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

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.

发布评论

评论列表(0)

  1. 暂无评论