Home >Backend Development >PHP Tutorial >There seems to be spaces in the string. How to delete them?
<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>
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)