php在指定字符串中查找子字符串

n342 9年前發布 | 765 次閱讀 PHP

對strpos()函數可以用來在php中查找子字符串。strpos()函數將試圖找到子字符串在源字符串中首次出現的位置。如果找到了,它會返回一個非負整數表示子字符串出現的位置。 否則它會返回一個布爾值false。

<?php
$haystack1 = "2349534134345w3mentor16504381640386488129";
$haystack2 = "w3mentor234953413434516504381640386488129";
$haystack3 = "center234953413434516504381640386488129fyi";
$pos1 = strpos($haystack1, "w3mentor");
$pos2 = strpos($haystack2, "w3mentor");
$pos3 = strpos($haystack3, "w3mentor");
print("pos1 = ($pos1); type is " . gettype($pos1) . "\n");
print("pos2 = ($pos2); type is " . gettype($pos2) . "\n");
print("pos3 = ($pos3); type is " . gettype($pos3) . "\n");
?>

輸出結果

pos1 = (13); type is integer
pos2 = (0); type is integer
pos3 = (); type is boolean

pos3返回的是bool值,即沒有找到子字符串</pre>

 本文由用戶 n342 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!