當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP strpbrk()用法及代碼示例

strpbrk()函數是PHP中的內置函數,它在字符串中搜索任何指定字符。此函數從發現任何指定字符的第一個匹配項的位置返回字符串的其餘部分。如果找不到任何字符,則返回false。此函數區分大小寫。

用法:

strpbrk( $string, $charlist)

參數:該函數接受兩個參數,如上麵的語法所示。這兩個參數都是強製性的,必須提供。所有這些參數如下所述:


  • $string: 此參數指定要搜索的字符串。
  • $charlist: 此參數指定要查找的字符。

返回值:此函數返回從找到的字符開始的字符串,如果找不到則返回false。

例子:

Input : $string = "Geeks for Geeks!", $charlist = "ef"
Output : eeks for Geeks!
Explanation : 'e' is the first occurrence of the specified 
characters. This function will, therefore, output "eeks for Geeks!", 
because it returns the rest of the string from where it found
the first occurrence of 'e'.


Input : $string = "A Computer Science portal", $charlist = "tue"
Output : uter Science portal

下麵的程序將說明PHP中的strpbrk()函數:

示例1:

<?php 
echo strpbrk("Geeks for Geeks!", "ef");  
?>

輸出:

eeks for Geeks!

示例2:

<?php 
echo strpbrk("A Computer Science portal", "tue");  
?>

輸出:

uter Science portal

示例3:該程序將說明該函數的區分大小寫。

<?php 
echo strpbrk("A Computer Science portal", "c");  
?>

輸出:

cience portal

參考:
http://php.net/manual/en/function.strpbrk.php



相關用法


注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 PHP | strpbrk() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。