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.
How about this:
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.
Works fine.
Try to use default trim function with "character_mask".
For example:
Read the official documentation http://php.net/manual/ru/function.trim.php
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:
would become:
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.
How about the following
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.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: