It seems to be a problem with magic quotes. The original string just contains \n and \n\n and \n\n\n and \n\r and so on. These newlines arent interpreted by the browser.
What we wanna do is: to replace more than 2 newlines with just 1 single \n.
What we tried yet: a lot of different regular expressions with preg_replace, but the \n wont be kicked out.
Any ideas?
Heres an example (updated on your suggestions – but still not working):
echo '<h3>Source:</h3>';
$arr_test = array(
'title' => 'my title',
'content' => 'thats my content\n\n\n\nwith a newline'
);
$json_text = json_encode($arr_test);
$json_text = stripslashes($json_text); //if I leave that out, then \\n will echo
echo $json_text;
// OUTPUT: {"title":"my title","content":"thats my content\n\n\n\nwith a newline"}
echo '<h3>Result 1:</h3>';
$pattern = '/\n{2,}/';
$result1 = preg_replace($pattern,"x",$json_text);
echo $result1;
// OUTPUT: {"title":"my title","content":"thats my content\n\n\n\nwith a newline"}
echo '<h3>Result 2:</h3>';
$result2 = preg_replace( '/([\n]+)/s', 'x', $json_text, -1, $count );
echo $count;
// OUTPUT: 0
echo $result2;
// OUTPUT: {"title":"my title","content":"thats my content\n\n\n\nwith a newline"}
You could also try looping through the string and replace two newlines with one until no double newline is left:
or with preg_replace: