Home >Backend Development >PHP Tutorial >There seems to be spaces in the string. How to delete them?

There seems to be spaces in the string. How to delete them?

WBOY
WBOYOriginal
2016-07-06 13:54:001222browse

<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>

Reply content:

<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>

Use $str = preg_replace('/^[pZpC] |[pZpC] $/u','',$str);

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

Used to match common character types when UTF-8 mode is selected

pC: All unicode "other"
pZ: All unicode "separator", all spaces and invisible characters

PHP merges multiple consecutive spaces:
$output = preg_replace('/s /', ' ', $input);
s matches any whitespace character, including spaces, tabs, form feeds, etc., which is equivalent to [ fnrtv].
http://php.net/manual/zh/reference.pcre.pattern.syntax.php

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

Urlencode to see what character it is, (is it a space or something else)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn