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
Release: 2016-07-06 13:54:00
Original
1218 people have browsed it

<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>
Copy after login
Copy after login
<code>如何去除空格?</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login
<code>如何去除空格?</code>
Copy after login
Copy after login

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)

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template