PHP程序从字符串中删除非字母数字字符
要从字符串中删除非字母数字字符,代码如下-
示例
<?php $my_str="Thisis!@sample*on&ly)#$"; $my_str = preg_replace( '/[^a-z0-9]/i', '', $my_str); echo "The non-alphanumeric characters removed gives the string as "; echo($my_str); ?>
输出结果
The non-alphanumeric characters removed gives the string as Thisissampleonly
函数“preg_replace”用于从字符串中删除字母数字字符。正则表达式用于过滤字母数字字符。该字符串是预先定义的,并在其上调用了函数“preg_replace”,并且重新格式化的字符串显示在控制台上。
示例
<?php $my_str="This!#is^&*a)(sample*+_only"; $my_str = preg_replace( '/[W]/', '', $my_str); echo "The non-alphanumeric characters removed gives the string as "; echo($my_str); ?>
输出结果
The non-alphanumeric characters removed gives the string as Thisisasample_only
唯一的区别是使用了不同的正则表达式。它的含义与先前的正则表达式相同,但编写方式不同。
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话