$string="This is testing para";
$string_withoue_whitespace=trim($string," ")开发者_Go百科;
My expcted output is Thisismypara
but whitespaces are not removed.
How can i remove white spaces within a paragraph in php
$string = "This is testing para";
$new_string = preg_replace('/( *)/','',$string);
echo $new_string;
or
$string = "This is testing para";
$new_string = str_replace(' ','',$string);
echo $new_string;
$string_without_whitespaces = str_replace(' ', '', $string);
Use str_replace() instead.
Also, if you are not sure there is only spaces, but some other non-printable chars, that look like a space - you can also use preg_replace():
$string_without_whitespaces = preg_replace('~\s~', '', $string);
精彩评论