(PHP 4, PHP 5)
stristr — strstr() 函數的忽略大小寫版本
說明
string stristr ( string$haystack
, mixed $needle
[, bool $before_needle
= false
] )
返回 haystack
字符串從 needle
第一次出現的位置開始到結尾的字符串。
參數
haystack
在該字符串中查找。
needle
如果 needle
不是一個字符串,那么它將被轉換為整型并被視為字符順序值。
before_needle
若為 TRUE
,strstr() 將返回 needle
在 haystack
中的位置之前的部分(不包括 needle)。
參數 needle
和 haystack
將以不區分大小寫的方式對待。
返回值
返回匹配的子字符串。如果 needle
未找到,返回 FALSE
。
更新日志
版本
說明
5.3.0
新增可選的 before_needle
參數。
4.3.0
stristr() 變為二進制安全的。
范例
Example #1 stristr() 范例
<?php
$email = 'USER@EXAMPLE.com';
echo stristr($email, 'e'); // 輸出 ER@EXAMPLE.com
echo stristr($email, 'e', true); // 自 PHP 5.3.0 起,輸出 US
?>
Example #2 測試字符串的存在與否
<?php
$string = 'Hello World!';
if(stristr($string, 'earth') === FALSE) {
echo '"earth" not found in string';
}
// 輸出: "earth" not found in string
?>
Example #3 使用非字符串 needle
<?php
$string = 'APPLE';
echo stristr($string, 97); // 97 = 小寫字母 a
// 輸出: APPLE
?>
注釋
Note: 此函數可安全用于二進制對象。
參見
strstr() - 查找字符串的首次出現 strrchr() - 查找指定字符在字符串中的最后一次出現 stripos() - 查找字符串首次出現的位置(不區分大小寫) strpbrk() - 在字符串中查找一組字符的任何一個字符 preg_match() - 執行一個正則表達式匹配