>  기사  >  백엔드 개발  >  字符串里貌似空格,如何删除?

字符串里貌似空格,如何删除?

WBOY
WBOY원래의
2016-07-06 13:54:001166검색

<code>$str ='                            Controllable Eu valence for photoluminescence tuning in apatite-typed';

在编码为GBK的情况下是'聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽Controllable Eu valence for photoluminescence tuning in apatite-typed';

在utf-8下是'                            Controllable Eu valence for photoluminescence tuning in apatite-typed';

</code>
<code>如何去除空格?</code>

回复内容:

<code>$str ='                            Controllable Eu valence for photoluminescence tuning in apatite-typed';

在编码为GBK的情况下是'聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽Controllable Eu valence for photoluminescence tuning in apatite-typed';

在utf-8下是'                            Controllable Eu valence for photoluminescence tuning in apatite-typed';

</code>
<code>如何去除空格?</code>

$str = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u','',$str);

http://php.net/manual/zh/regexp.reference.unicode.php

在选用 UTF-8模式时用于匹配通用字符类型

pC:所有的unicode“other”
pZ:所有的unicode“separator” ,所有空格和不可见字符

PHP合并多个连续的空格:
$output = preg_replace('/\s+/', ' ', $input);
\s 匹配任何空白字符,包括空格、制表符、换页符等等,等价于[ \f\n\r\t\v]
http://php.net/manual/zh/reference.pcre.pattern.syntax.php

$str = $str.replace(/s/g,'');

urlencode一下看看是什么字符,(是空格还是其他的)

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.