在php编程中,strstr()函数的作用是:返回一个字符串在另一个字符串中首次出现的位置到后者末尾的子字符串(大小写敏感)。
php 判断字符串是否包含其它字符
以下几个函数均可用来判断某字符串是否包含另外一个字符串php 中判断一个字符串是否包含其它字符是很常见的操作。
例子:
 
我觉得最简单的就是这种了 strpos($a, $b) !== false 如果$a 中存在 $b,则为 true ,否则为 false。 
用 !== false (或者 === false) 的原因是如果 $b 正好位于$a的开始部分,那么该函数会返回int(0),那么0是false,但$b确实位于$a中,所以要用 !== 判断一下类型,要确保是严格的 false。昨天晚上去中关村图书大厦,看到一本书中用的是 strpos === true 来判断,这是极其不正确的。。。 
出错的书为《php求职宝典》107页(2012-02-26更新) 
其它的还有 php 原生支持的函数,如 strstr(),stristr() 等,直接判断就可以了。
定义和用法 
strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。
该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。
语法 
strstr(string,search)
参数 描述 
string 必需。规定被搜索的字符串。 
search 必需。规定所搜索的字符串。如果该参数是数字,则搜索匹配数字 ascii 值的字符。
提示和注释 
注释:该函数是二进制安全的。
注释:该函数对大小写敏感。如需进行大小写不敏感的搜索,请使用 stristr()。
例子 1 
 
例子 2 
将搜索 "o" 的 ascii 值所代表的字符: 
 
例子 3 
 
<?php 
$email = 'admin@jb200.com'; 
$domain = strstr($email, '@'); 
echo $domain; // prints @jb51.net
$user = strstr($email, '@', true); // as of php 5.3.0 
echo $user; // prints admin
$city_str=fopen(cgi_path."/data/weather/city.dat","r"); 
$city_ch=fread($city_str,filesize(cgi_path."/data/weather/city.dat")); 
$city_ch_arr=explode("|",$city_ch); 
//如果能匹配到所在市 
if(strstr($area_ga,"市")){ 
foreach($city_ch_arr as $city_ch_arr_item){ 
if(@strstr($area_ga,$city_ch_arr_item)){ 
echo $area_ga.'<br>'; 
echo $city_ch_arr_item; 
$s_city=$city_ch_arr_item; 
} 
} //脚本学堂 http://www.jb200.com
} 
//如果找不到市 那么看看是不是能找到省 有时会有这样的情况:广东省长城宽带 这样的一律归属到该省省府 
elseif(strstr($area_ga,"河北")!==false){ 
$s_city="石家庄"; 
}elseif(strstr($area_ga,"福建")!==false){ 
$s_city="福州"; 
}elseif(strstr($area_ga,"台湾")!==false){ 
$s_city="台北"; 
}elseif(strstr($area_ga,"香港")!==false){ 
$s_city="香港"; 
}elseif(strstr($area_ga,"广西")!==false){ 
$s_city="南宁"; 
}elseif(strstr($area_ga,"浙江")!==false){ 
$s_city="杭州"; 
}elseif(strstr($area_ga,"江苏")!==false){ 
$s_city="南京"; 
}elseif(strstr($area_ga,"山东")!==false){ 
$s_city="济南"; 
}elseif(strstr($area_ga,"安徽")!==false){ 
$s_city="合肥"; 
}elseif(strstr($area_ga,"湖南")!==false){ 
$s_city="长沙"; 
}elseif(strstr($area_ga,"四川")!==false){ 
$s_city="成都"; 
}elseif(strstr($area_ga,"云南")!==false){ 
$s_city="昆明"; 
}elseif(strstr($area_ga,"广东")!==false){ 
$s_city="广州"; 
}elseif(strstr($area_ga,"贵州")!==false){ 
$s_city="贵阳"; 
}elseif(strstr($area_ga,"西藏")!==false){ 
$s_city="拉萨"; 
}elseif(strstr($area_ga,"新疆")!==false){ 
$s_city="乌鲁木齐"; 
}elseif(strstr($area_ga,"蒙古")!==false){ 
$s_city="呼和浩特"; 
}elseif(strstr($area_ga,"黑龙江")!==false){ 
$s_city="哈尔滨"; 
}elseif(strstr($area_ga,"辽宁")!==false){ 
$s_city="沈阳"; 
}elseif(strstr($area_ga,"吉林")!==false){ 
$s_city="长春"; 
}elseif(strstr($area_ga,"河南")!==false){ 
$s_city="郑州"; 
}elseif(strstr($area_ga,"湖北")!==false){ 
$s_city="武汉"; 
}elseif(strstr($area_ga,"山西")!==false){ 
$s_city="太原"; 
}elseif(strstr($area_ga,"陕西")!==false){ 
$s_city="西安"; 
}elseif(strstr($area_ga,"甘肃")!==false){ 
$s_city="兰州"; 
}elseif(strstr($area_ga,"宁夏")!==false){ 
$s_city="银川"; 
}elseif(strstr($area_ga,"海南")!==false){ 
$s_city="海口"; 
}elseif(strstr($area_ga,"江西")!==false){ 
$s_city="南昌"; 
}elseif(strstr($area_ga,"澳门")!==false){ 
$s_city="澳门"; 
} 
//如果都不存在 就是默认显示广州 比如本地机 
else{ 
$s_city="广州"; 
}
如上代码: 
其中 city.dat中是一些城市 格式是这样的 
 
参考例子:
 
php字符串查找函数(strrpos与strchr)
php字符串查找函数strstr()、strrchr()实例
php字符串比较与查找方法详解
php查找、替换字符串中http地址的代码分享
查找字符串中是否包含某些字符的函数strstr及其它
php中用于查找字符串的常用函数