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 妈妈生日祝福语简短励志