You can use a combination of trim to remove leading and trailing whitespace, and preg_replace to replace all newlines and their surrounding spaces internal to the string with a single space:
$str = preg_replace('/\s*\R\s*/', ' ', trim($str));
echo $str;
Note in the above regex \R matches any newline (\r, \n) character.
Output:
This Like a Somthing awesome text with goods:Fb,Teleg,Top,Prods.fm,Ad-...
Demo on 3v4l.org
Answer from Nick on Stack OverflowCan trim() remove newline characters?
Does trim() affect UTF-8 or multibyte strings?
The problem is that you're escaping the \n for MySQL first. Try this:
<?php
if (isset ($_POST['comment_form_submit'])) {
$body = $_POST['body'];
$breaks = array("\r\n", "\n", "\r");
$newtext = str_replace($breaks, "", $body);
$newtext = mysql_real_escape_string($newtext);
echo $newtext;
}
?>
Read about the escape function you're using to see why: http://php.net/mysql_real_escape_string
Please use this below code, Or copy paste the content in CK editor and replace the character which u want to replace...
<?php
$desc = trim(stripslashes($crw['Description']));
$desc=str_replace(","," ",$desc);
$desc=str_replace("\r\n"," ",$desc);
$desc=str_replace("\n"," ",$desc);
$desc=str_replace("<br>"," ",$desc);
$desc=str_replace("<br />"," ",$desc);
echo $desc; ?>
Ben's solution is acceptable, but str_replace() could be faster than preg_replace()
$buffer = str_replace(array("\r", "\n"), '', $buffer);
You should be able to replace it with a preg that removes all newlines and carriage returns. The code is:
preg_replace( "/\r|\n/", "", $yourString );
Even though the \n characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.