1

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"}
2
  • I think the second regular expression you tried has a typo on the fourth character! (aka: please show some of your work and the results, so it is easy to see if it's just an error in the expression, or something different (strange stuff in the json)) Commented Apr 3, 2012 at 12:52
  • What have you tried? And did you include /s modifier which includes new lines? Commented Apr 3, 2012 at 12:57

5 Answers 5

1

You could also try looping through the string and replace two newlines with one until no double newline is left:

echo '<h3>Result 4:</h3>';
$result4 = $json_text;
do{
    $result4 = str_replace('\n\n','\n',$result4, $count);
}while($count>0);

echo $result4;
// OUTPUT: {"title":"my title","content":"thats my content\nwith a newline"}

or with preg_replace:

echo '<h3>Result 5:</h3>';

$result5 = preg_replace('/(\\\n)+/m', '\\\n', $json_text);

echo $result5;
// OUTPUT: {"title":"my title","content":"thats my content\nwith a newline"}
Sign up to request clarification or add additional context in comments.

Comments

1
if(get_magic_quotes_gpc()) {
   $string =  stripslashes($string); // $string sended with POST or GET 
}

$string = str_replace("\n\n", "\n", $string); // only for 2 newlines

OR

$string = preg_replace('/\n{2,}/s', '\n', $string); // more than 2 newlines

3 Comments

If you replace 2 with 1, what happens when there are three? Three goes to two, so you would need to perform the replace twice.
@ChrisGessler second solution with preg_replace works for more than 2
without the /s modifier, how will go beyond one line?
0

try this:

// replace 2 or more consecutive newlines with a single newline
$string = preg_replace("/\n\n+/i", "\n", $string);

Comments

0

Try this:

  1. Replace all \n characters (1 or more) with \n
  2. /s modifier - include multiple lines
>     $string = preg_replace( $'/([\n]+)/s', '\n', $string, -1, $count );                                               
>     echo $count;

Comments

0
echo str_replace("\n", "", "aassasa\n \n aasassf \n saaaaafs asaf ssaf \n afsf \n ");

just for your demo

echo str_replace("a","","aaabcefefgaaaaaaaaaaamnopqraaaaaa");

2 Comments

doesn't this simply replace all \n characters with empty string?
how does it leave just 1 \n when multiples are present? i.e. \n\n\n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.