PHP程序检查字符串是否具有特殊字符
要检查字符串是否具有特殊字符,PHP代码如下:
示例
<?php
function check_string($my_string){
$regex = preg_match('[@_!#$%^&*()<>?/|}{~:]', $my_string);
if($regex)
print("String has been accepted");
else
print("String has not been accepted");
}
$my_string = 'This_is_$_sample!';
check_string($my_string);
?>输出结果
String has not been accepted
上面定义了一个名为“check_string”的函数,该函数将字符串作为其参数-
$my_string = 'This_is_$_sample!';
使用正则表达式检查字符串是否具有特殊字符。如果有特殊字符,则会打印一条特定消息。在函数外部,定义字符串,并通过将字符串作为参数传递来调用函数-
function check_string($my_string){
$regex = preg_match('[@_!#$%^&*()<>?/|}{~:]', $my_string);
if($regex)
print("String has been accepted");
else
print("String has not been accepted");
}热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短