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
唯一的区别是使用了不同的正则表达式。它的含义与先前的正则表达式相同,但编写方式不同。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短