如何检查PHP的字符串中是否只有空格? [英] How to check if there are only spaces in string in PHP?

查看:824
本文介绍了如何检查PHP的字符串中是否只有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

print_r(strlen(trim('     ')));

结果是9

我也尝试过

preg_replace('/[\n\r\t\s]/', '', '   ')

但结果不为零.

请下载我的代码,您将获得结果

http://blog.eood.cn/attachment.php?id=70

推荐答案

mb_language('uni');
mb_internal_encoding('UTF-8');

$s = '     ';
if (strlen(preg_replace('/\s+/u','',$s)) == 0) {
    echo "String is empty.\n";
}

如果这不起作用,我建议您这样做

If that doesn't work i suggest doing this

$s = '     ';
if (strlen(trim(preg_replace('/\xc2\xa0/',' ',$s))) == 0) {
    echo "String is empty.\n";
}

这些解决方案已在不同平台上进行了测试.

These solutions have been tested on different platforms.

u 标志告诉preg_replace()可以将字符串视为多字节字符串,即utf-8

The u flag tells preg_replace() to treat the string as a multibyte string, namely utf-8

该字符是不间断空格C2A0,可以用alt + 0160生成.

The character is a nonbreaking space C2A0 and can be generated with alt+0160.

这篇关于如何检查PHP的字符串中是否只有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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