php判断字符与字符串的包含,即用php判断字符串格式,可以使用PHP的内置函数strstr,strpos,stristr直接进行判断。
也可以通过explode函数实现一个自定义的判断函数。
方法分享:
1. strstr: 返回一个从被判断字符开始到结束的字符串,如果没有返回值,则不包含
复制代码 代码示例:
/*php手册上的例子*/
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?>
2. stristr:
与strstr的使用方法完全一样.唯一的区别是stristr不区分大小写.
3. strpos:
返回boolean值.FALSE和TRUE不用多说.用 “===”进行判断.strpos在执行速度上都比以上两个函数快。
另外,strpos有一个参数指定判断的位置,但是默认为空,即判断整个字符串。
缺点,对中文的支持不好
使用方法:
复制代码 代码示例:
$str= 'abc';
$needle= 'a';
$pos = strpos($str, $needle);
4,用explode进行判断
例子:
复制代码 代码示例:
function checkstr($str){
$needle = "a";//判断是否包含a这个字符
$tmparray = explode($needle,$str);
if(count($tmparray)>1){
return true;
} else{
return false;
}
}