数据库blob拥有多行html代码,我怎么能用php将它转换成一行

时间:2009-08-05 01:51:11

标签: php javascript regex

基本上我使用fckeditor作为我的用户输入一些信息的方式。当它存储在数据库中时,它就像这样存储,

<p> </p>
<div style=\"margin: 0in 0in 10pt\"><b><span style=\"color: #666666\">Test Title</span></b><span style=\"color: #666666\"> <br />
Address goes here <br />
This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location.</span></div>

所以无论如何,当我输出这个文本时,它被发送到一个函数,它接受它并在谷歌地图上创建一个精确点,该文本进入弹出窗口。我遇到的问题是,当php输出javascript函数调用时,它看起来像这样,

showAddress(" <p> </p>
<div style=\"margin: 0in 0in 10pt\"><b><span style=\"color: #666666\">Test Title</span></b><span style=\"color: #666666\"> <br />
Address goes here <br />
This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location.</span></div>"); 

换行后的空格,基本上只要你按Enter键,就会导致javascript以'未终止的字符串文字'退出。

4 个答案:

答案 0 :(得分:2)

$value = str_replace(array("\n", "\r"), "", $string);

更新,因为Andrew Moore想到了\ r \ n键。但是,您不需要在replace参数中使用数组。

这将取代所有无效的新行。

答案 1 :(得分:2)

使用str_replace()删除新行:

str_replace(array("\n", "\r"), '', $someString);

此外,由于您要存储文本,因此最好使用TEXT数据类型而不是BLOB,因为TEXT可识别区域设置。

答案 2 :(得分:1)

我认为你想留一个空格而不是空字符串,所以文本不只是一起运行。

作为perl遗产,我会使用

$ cleaningString = preg_replace(“/ [\ n \ r] + /”,“”,$ stringWithReturns);

这将捕获多个返回并替换为单个空格。

答案 3 :(得分:1)

您应该使用json_encode来确保您的HTML正确进入javascript:

<script>
showAddress(<?php echo json_encode($htmlFromDB); ?>);
</script>

Newlines应该不再是问题。