开发者

How to remove space from para in php

开发者 https://www.devze.com 2023-01-31 04:47 出处:网络
$string=\"This is testing para\"; $string_withoue_whitespace=trim($string,\" \")开发者_Go百科; My expcted output is Thisismypara but whitespaces are not removed.
$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);
0

精彩评论

暂无评论...
验证码 换一张
取 消