這篇文章主要介紹了php實現指定字符串中查找子字符串的方法,涉及php中strpos()函數查找字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了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值,即沒有找到子字符串
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答