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 66红包祝福语简短的
11 鼠年元旦生日祝福语简短
12 过年拜年祝福语大全简短
13 对当兵的祝福语简短
14 交个朋友蛋糕祝福语简短
15 艾伦老师祝福语简短
16 离别赠言离职祝福语简短
17 画室揭牌仪式祝福语简短
18 儿子立冬祝福语简短独特