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 祝福语驱散霉运的话简短