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

查看:92
本文介绍了php - 字符串里貌似空格,如何删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

$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';

如何去除空格?

解决方案

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

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

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

pC:所有的unicodeother
pZ:所有的unicodeseparator ,所有空格和不可见字符

这篇关于php - 字符串里貌似空格,如何删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆