duansha8764 2011-01-23 02:28
浏览 39
已采纳

如何使用PHP或JS删除换行符

I've tried about everything to delete some extra 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(/
/g,"")

In PHP:

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

and when I try

 text.replace(/
/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 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(' ', '', $result)

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

Looks identical right? Well it seems that when you enclose a string with a character value like in double quotes " " it's seen as it's character value NOT as a string. On the other hand if you enclose it in single quotes ' ' it's really seen as the string . 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.

  • 写回答

4条回答 默认 最新

  • dongping5230 2011-01-23 03:15
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效