stristr

(PHP 4, PHP 5, PHP 7, PHP 8)

stristrstrstr() 函數(shù)的忽略大小寫版本

說明

stristr(string $haystack, mixed $needle, bool $before_needle = false): string

返回 haystack 字符串從 needle 第一次出現(xiàn)的位置開始到結(jié)尾的字符串。

參數(shù)

haystack

在該字符串中查找。

needle

如果 needle 不是一個字符串,那么它將被轉(zhuǎn)換為整型并被視為字符順序值。

before_needle

若為 truestrstr() 將返回 needlehaystack 中的位置之前的部分(不包括 needle)。

參數(shù) needlehaystack 將以不區(qū)分大小寫的方式對待。

返回值

返回匹配的子字符串。如果 needle 未找到,返回 false。

更新日志

版本 說明
5.3.0 新增可選的 before_needle 參數(shù)。
4.3.0 stristr() 變?yōu)槎M制安全的。

范例

示例 #1 stristr() 范例

<?php
  $email 
'USER@EXAMPLE.com';
  echo 
stristr($email'e'); // 輸出 ER@EXAMPLE.com
  
echo stristr($email'e'true); // 自 PHP 5.3.0 起,輸出 US
?>

示例 #2 測試字符串的存在與否

<?php
  $string 
'Hello World!';
  if(
stristr($string'earth') === FALSE) {
    echo 
'"earth" not found in string';
  }
// 輸出: "earth" not found in string
?>

示例 #3 使用非字符串 needle

<?php
  $string 
'APPLE';
  echo 
stristr($string97); // 97 = 小寫字母 a
// 輸出: APPLE
?>

注釋

注意: 此函數(shù)可安全用于二進制對象。

參見

  • strstr() - 查找字符串的首次出現(xiàn)
  • strrchr() - 查找指定字符在字符串中的最后一次出現(xiàn)
  • stripos() - 查找字符串首次出現(xiàn)的位置(不區(qū)分大小寫)
  • strpbrk() - 在字符串中查找一組字符的任何一個字符
  • preg_match() - 執(zhí)行匹配正則表達式