栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP:如何删除字符串中所有不可打印的字符?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP:如何删除字符串中所有不可打印的字符?

7位ASCII?

如果您的Tardis刚好在1963年登陆,并且您只想要7位可打印的ASCII字符,则可以使用以下方法从0-31和127-255中删除所有内容:

$string = preg_replace('/[x00-x1Fx7F-xFF]/', '', $string);

它匹配0-31、127-255范围内的任何内容并将其删除。

8位扩展ASCII?

您掉进了热水浴缸计时机,而您又回到了八十年代。如果您使用某种形式的8位ASCII,则可能需要将字符保持在128-255范围内。轻松调整-只需查找0-31和127

$string = preg_replace('/[x00-x1Fx7F]/', '', $string);

UTF-8?

啊,欢迎回到21世纪。如果您使用UTF-8编码的字符串,则可以在正则表达式上使用

/u
修饰符

$string = preg_replace('/[x00-x1Fx7F]/u', '', $string);

这只会删除0-31和127。这可用于ASCII和UTF-8,因为它们共享相同的控件集范围(如下面的mgutt所述)。严格来说,如果没有

/u
修饰符,这将起作用。但是,如果您要删除其他字符,它将使生活更加轻松…

在UTF-8字符串中,该编码为

0xC2A0
。您可以查找并删除该特定序列,但是
/u
只要有了修饰符,您就可以简单地添加
xA0
到字符类中:

$string = preg_replace('/[x00-x1Fx7FxA0]/u', '', $string);

附录:str_replace呢?

preg_replace是非常有效的,但是如果您经常执行此操作,则可以构建要删除的字符数组,并使用下面的mgutt指出的str_replace,例如

//build an array we can re-use across several operations$badchar=array(    // control characters    chr(0), chr(1), chr(2), chr(3), chr(4), chr(5), chr(6), chr(7), chr(8), chr(9), chr(10),    chr(11), chr(12), chr(13), chr(14), chr(15), chr(16), chr(17), chr(18), chr(19), chr(20),    chr(21), chr(22), chr(23), chr(24), chr(25), chr(26), chr(27), chr(28), chr(29), chr(30),    chr(31),    // non-printing characters    chr(127));//replace the unwanted chars$str2 = str_replace($badchar, '', $str);

直觉上,这似乎会很快,但并非总是如此,您绝对应该进行基准测试,看看它是否可以为您节省任何费用。我使用随机数据在各种字符串长度上进行了一些基准测试,并且使用php7.0.12出现了这种模式

     2 chars str_replace     5.3439ms preg_replace     2.9919ms preg_replace is 44.01% faster     4 chars str_replace     6.0701ms preg_replace     1.4119ms preg_replace is 76.74% faster     8 chars str_replace     5.8119ms preg_replace     2.0721ms preg_replace is 64.35% faster    16 chars str_replace     6.0401ms preg_replace     2.1980ms preg_replace is 63.61% faster    32 chars str_replace     6.0320ms preg_replace     2.6770ms preg_replace is 55.62% faster    64 chars str_replace     7.4198ms preg_replace     4.4160ms preg_replace is 40.48% faster   128 chars str_replace    12.7239ms preg_replace     7.5412ms preg_replace is 40.73% faster   256 chars str_replace    19.8820ms preg_replace    17.1330ms preg_replace is 13.83% faster   512 chars str_replace    34.3399ms preg_replace    34.0221ms preg_replace is  0.93% faster  1024 chars str_replace    57.1141ms preg_replace    67.0300ms str_replace  is 14.79% faster  2048 chars str_replace    94.7111ms preg_replace   123.3189ms str_replace  is 23.20% faster  4096 chars str_replace   227.7029ms preg_replace   258.3771ms str_replace  is 11.87% faster  8192 chars str_replace   506.3410ms preg_replace   555.6269ms str_replace  is  8.87% faster 16384 chars str_replace  1116.8811ms preg_replace  1098.0589ms preg_replace is  1.69% faster 32768 chars str_replace  2299.3128ms preg_replace  2222.8632ms preg_replace is  3.32% faster

计时本身是10000次迭代,但更有趣的是相对差异。最多512个字符,我一直看到preg_replace总是赢。在1-8kb的范围内,str_replace具有边沿边缘。

我认为这是一个有趣的结果,因此将其包含在此处。 重要的不是获取此结果并使用它来决定使用哪种方法,而是对照您自己的数据进行基准测试然后再决定。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430484.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号