Search code examples
phpoutput-buffering

Removing newlines from ob_get_clean() output


ok i do have this following codes

<?php
ob_start();
?>
codepad is an 
online compiler/interpreter, 
and a simple collaboration tool.
Paste 

your code below, 
and codepad wi
ll run 
it and give you a short 
URL you can use to share
 it in chat or email
<?php
$str = str_replace('\r\n','',trim(ob_get_clean()));
echo $str;
?>

and you can see how it works here http://codepad.org/DrOmyoY9

now what i want here is to remove the newlines from the stored output of the ob_get_clean().

I almost looked around the internet on how to remove newlines in the strings and that's the common and fastest method to remove the newlines aside from using the slowly preg_replace().

Why this happens? is this already a bug? or i just missed something?


Solution

  • \r\n is windows style, but if the user using linux or mac, it would be different . so best solution is:

    $str = str_replace(array("\r","\n"),'',trim(ob_get_clean()));