php判断字符以及字符串的包含方法属性
更新时间:2008年08月30日 19:16:26 作者:
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;
}
}
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;
}
}
相关文章
php中目录操作opendir()、readdir()及scandir()用法示例
这篇文章主要介绍了php中目录操作opendir()、readdir()及scandir()用法,结合具体实例形式分析了PHP使用opendir()、readdir()及scandir()读取目录的相关操作技巧,需要的朋友可以参考下2019-06-06
PHP数组的交集array_intersect(),array_intersect_assoc(),array_inte
求两个数组的交集问题可以使用array_intersect(),array_inersect_assoc,array_intersect_key来实现,其中array_intersect()函数是求两个数的交集2011-05-05
PHP运行出现Notice : Use of undefined constant 的完美解决方案分享
今天修改公司的网站,提示Notice : Use of undefined constant ,通过下面的方法解决了,最好是error_reporting(0);不需要更改配置2012-03-03
php使用simplexml_load_file加载XML文件并显示XML的方法
这篇文章主要介绍了php使用simplexml_load_file加载XML文件并显示XML的方法,实例分析了simplexml_load_file操作XML文件的技巧,非常具有实用价值,需要的朋友可以参考下2015-03-03


最新评论