2012-03-04 107 views
11

我做了一个功能,删除换行符与PHP没有成功,我tryed所有替换代码,我仍然得到这些换行符,我创建一个JSON文件,我不能用jquery从jsonp中读取它,因为这些换行符似乎打破了这一切。PHP删除换行符或CR LF没有成功

function clean($text) 
{ 
$text = trim(preg_replace('/\s+/', ' ', $text)); 
$text = preg_replace("/(\r\n|\n|\r|\t)/i", '', $text); 
return $text; 
} 

当我看源,有一些换行符在所有HREF,IMG和Br appening这是一个json_encode输出 例如:

<a 
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\"> 

换行符AFER一个。 它hapenig到IMG SRC和BR

的唯一途径,我可以删除这些打破它与

$text = preg_replace("/\s/i", '', $text); 

但你understant,有没有留在所有字符串的空间,这不是我们想要的。

+0

感谢的为所有,但到现在为止,这些解决方案的工作。但通常大多数这些解决方案应该工作,但我不明白为什么我的情况下它不 – Gino 2012-03-12 15:32:55

+0

我发现修复它的唯一方法是 '$ allcontent = preg_replace('/ Gino 2012-03-22 03:53:14

+0

I hit the same and no solutions in this post worked for me. I found another similar post and this solution worked for me...hence posting it here: http://stackoverflow.com/a/3340510/405117 – Vikram 2012-08-01 20:39:45

回答

0

使用json_encode()json_decode()从JSON扩展处理JSON德/序列化任务:

$myobj = array('foo' => 'bar', 'foz' => 'baz') 

$json_myobj = json_encode($myobj); 
echo $json_myobj; 

$myobj = json_decode($json_myobj); 
print_r($myobj); 
25

这种更换工作更好地为我:

= str_replace (array("\r\n", "\n", "\r"), ' ', $text) 
+0

As a note, in the case of e-mails and other MIME encodings, simply replacing '\r\n' (CRLF) with '\n' should work fine. – 2013-05-03 17:47:45

0

也许你可以尝试通过遍历文本字符字符和每个电话ord(),所以你可以看到这些突破字符是否真的\r,\n

最近我遇到了一个类似的问题,其结果是一个不可破坏的空间,甚至没有在ASCII表(ord代码194或其他)中的空白。

如果你有兴趣我的解决办法是要设法过滤器断裂,但过滤除什么预计将在文本,像这样的一切:

$text = preg_replace("/[^ \na-zа-я0-9`~\[email protected]#\$%\^&\*\(\)_\+\-\=\[\]\{\}\\\|;\:'\",\.\/\<\>\?]+/ui", "", $text); 
+0

hi Tony, this did nothing for me – Gino 2012-03-04 22:57:02

1

如何:

function clean($text) 
{ 
    $parts = explode(' ', $text); 
    foreach ($parts as $key => $value) 
     $parts[$key] = preg_replace('/\s/', ' ', $value); 
    return implode(' ', $parts); 
} 

事实上,如果不是像这样清理JSON文件,您可以使用json_encode来创建它,您将在之前的步骤中解决此问题。

+0

Oops, the original code had a big mistake. I have edited right now... – 2012-03-04 21:21:57

+0

not working, i don't understand why with all these replace code my json output give me 14 line instead of one. my json output work on many domains and many host, but for this domain (Wordpress website) it give me line break – Gino 2012-03-04 23:02:33

0

我用的方法是echo str_replace(array('\r\n', '\r', '\n', '\t'), array('\\r\\n', '\\r', '\\n', '\\t'), $text);

这样做是什么让你看到什么字符导致断裂的文字,并适当地进行更换。例如,如果你有一个“\ n”破坏你的文本,当你使用这段代码时,它会在它的位置显示一个“\ n”。例如:

<a 
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\"> 

将成为:

<a\n href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\"> 

自然地,有可以使用的大量的其他打破字符,但\ r \ N,\ r \ n和\ t为最常用的那些。

+0

it's crazy but nothing apear. by the way, the json_encode is called this way 'echo $_GET['jsoncallback'].'('.json_encode(array('posts' => $ posts))'。) “;' – Gino 2012-03-04 23:50:55

1

如何以下

function clean($text) 
{ 
    return trim(preg_replace("/(\s*[\r\n]+\s*|\s+)/", ' ', $text)); 
} 

第一部分\s*[\r\n]+\s*将取代任何换行符,它的前导空格和它的拖尾空间逼到一个空间。

第二部分\s+将空间缩小到一个空间。

然后trim()删除前导/尾随空间。

0
function clean($text) 
{ 
    return trim(preg_replace('/\\\\r|\\\\n|\\\\t/i', ' ', $text)); 
} 

工作正常。

0

如果你想删除的CR和保持的LF,这真的很简单(只是常识):

$text = str_replace("\r", "", $text);