【函数功能】
该函数搜索字符串在指定字符串中的第一次出现,不区分大小写,若要区分请选择strstr()函数。
【函数语法】
stristr ($haystack, $needle, $before_needle = null)
【参数说明】
$haystack:必需,要被搜索的字符串。
$needle:必需,要搜索的字符串,如果此参数是数字,则搜索匹配此数字对应的 ASCII 值的字符。
$before_needle:可选,默认值为 "false" ,如果设置为 "true",它将返回 $needle 参数第一次出现之前的字符串部分。
【演示程序】
<?php
/**
* stristr ($haystack, $needle, $before_needle = null)
* **/
$haystack = "Hello I am PHP!";
echo stristr($haystack, "i");//采用默认参数,即返回找到的字符串后面的部分
echo "<br>";
echo stristr($haystack, "i",true);//返回前面部分
echo "<br>";
echo stristr($haystack, 105);//采用ASCII码
?>
【输出结果】
I am PHP!
Hello
I am PHP!
【特别注意】
1.该函数是二进制安全的。
2.该函数对大小写不敏感。
3.PHP 版本:4+。
4.更新日志:在 PHP 5.3 中,新增了 before_search 参数。
5.若未找到指定字符串则返回false。
【原版定义】
/**
* Case-insensitive <function>strstr</function>
* @link http://www.php.net/manual/en/function.stristr.php
* @param haystack string <p>
* The string to search in
* </p>
* @param needle mixed <p>
* If needle is not a string, it is converted to
* an integer and applied as the ordinal value of a character.
* </p>
* @param before_needle bool[optional] <p>
* If true, stristr
* returns the part of the haystack before the
* first occurrence of the needle (excluding needle).
* </p>
* @return string the matched substring. If needle is not
* found, returns false.
*/
转载请注明出处:php1234.cn ,原文地址:http://www.php1234.cn/a/functions/2016/1012/91.html