php判断字符以及字符串的包含方法属性

php判断字符以及字符串的包含,可以使用PHP的内置函数strstr,strpos,stristr直接进行判断.也可以通过explode函数的作用写一个判断函数
 
 
下面介绍使用方法:

1. strstr: 返回一个从被判断字符开始到结束的字符串,如果没有返回值,则不包含
1 <?php 
2 /*如手册上的举例*/ 
3 $email = 'user@example.com'; 
4 $domain = strstr($email, '@'); 
5 echo $domain; // prints @example.com 
6 ?> 

 



2. stristr: 它和strstr的使用方法完全一样.唯一的区别是stristr不区分大小写.

3. strpos: 返回boolean值.FALSE和TRUE不用多说.用 “===”进行判断.strpos在执行速度上都比以上两个函数快,另外strpos有一个参数指定判断的位置,但是默认为空.意思是判断整个字符串.缺点是对中文的支持不好.使用方法
1 $str= 'abc'; 
2 $needle= 'a'; 
3 $pos = strpos($str, $needle); 

 



4. 用explode进行判断
1 function checkstr($str){ 
2 $needle = "a";//判断是否包含a这个字符 
3 $tmparray = explode($needle,$str); 
4 if(count($tmparray)>1){ 
5 return true; 
6 } else{ 
7 return false; 
8 } 
9 } 

 


posted on 2015-10-21 12:00  ziyi_ang  阅读(118)  评论(0编辑  收藏  举报

导航