可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I did a function to remove line break with php with no success, i tryed all replace code and i still get these line break, i create a json file and i can't read it from jsonp with jquery because of these line break seem to break it all.
function clean($text)
{
$text = trim( preg_replace( '/\s+/', ' ', $text ) );
$text = preg_replace("/(\r\n|\n|\r|\t)/i", '', $text);
return $text;
}
When i look at the source, there some line break appening in all href, img and br
this is a json_encode output
example:
<a
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\">
line break afer a.
it's hapenig to img src and br
the only way i can remove these break it with
$text = preg_replace("/\s/i", '', $text);
But you understant that there's no space left in all the string and it's not what we want.
回答1:
this replace works better for me:
= str_replace (array("\r\n", "\n", "\r"), ' ', $text)
回答2:
How about this:
function clean($text)
{
$parts = explode(' ', $text);
foreach ($parts as $key => $value)
$parts[$key] = preg_replace('/\s/', ' ', $value);
return implode(' ', $parts);
}
Indeed, if instead of cleaning the JSON file like this, you can use json_encode to create it, you will get rid this problem in a previous step.
回答3:
How about the following
function clean($text)
{
return trim(preg_replace("/(\s*[\r\n]+\s*|\s+)/", ' ', $text));
}
the first part \s*[\r\n]+\s*
will replace any line breaks, it's leading spaces and it's tailing spaces into just one space.
the second part \s+
will shrink spaces into one space.
then trim()
removes the leading/tailing space.
回答4:
Try to use default trim function with "character_mask".
For example:
$text = trim($text, " \t\n\r\0\x0B");
Read the official documentation http://php.net/manual/ru/function.trim.php
回答5:
use json_encode() and json_decode() from the JSON extension to handle JSON de/serialization tasks:
$myobj = array( 'foo' => 'bar', 'foz' => 'baz')
$json_myobj = json_encode($myobj);
echo $json_myobj;
$myobj = json_decode($json_myobj);
print_r($myobj);
回答6:
Maybe you could try walking the text character by character and call ord()
on each, so you could see if these break characters are really \r,\n
s?
Recently I got a similar problem with a whitespace which turned out to be a non-breakable space not even inside the ASCII table (ord code 194 or something).
If you are interested my solution was not to try and filter breaks, but to filter EVERYTHING except what was expected to be in text, like so:
$text = preg_replace("/[^ \na-zа-я0-9`~\!@#\$%\^&\*\(\)_\+\-\=\[\]\{\}\\\|;\:'\",\.\/\<\>\?]+/ui", "", $text);
回答7:
A method I use is echo str_replace(array('\r\n', '\r', '\n', '\t'), array('\\r\\n', '\\r', '\\n', '\\t'), $text);
What this does is allows you to see what characters are causing the breaks in the text, and replace them appropriately. For example, if you have a "\n" breaking your text, when you use this code, it will then display a "\n" in its place. Example:
<a
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\">
would become:
<a\n href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\">
Naturally, there are a large amount of other breaking characters that could be used, but \r\n, \r, \n and \t are the ones most commonly used.
回答8:
function clean($text)
{
return trim(preg_replace('/\\\\r|\\\\n|\\\\t/i', ' ', $text));
}
Works fine.
回答9:
If you want to remove the CRs and keep the LFs, it's really very simple (just common sense):
$text = str_replace("\r", "", $text);