php實現(xiàn)指定字符串中查找子字符串的方法

字號:


    這篇文章主要介紹了php實現(xiàn)指定字符串中查找子字符串的方法,涉及php中strpos()函數(shù)查找字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    本文實例講述了php實現(xiàn)指定字符串中查找子字符串的方法。分享給大家供大家參考。具體分析如下:
    對strpos()函數(shù)可以用來在php中查找子字符串。strpos()函數(shù)將試圖找到子字符串在源字符串中首次出現(xiàn)的位置。如果找到了,它會返回一個非負(fù)整數(shù)表示子字符串出現(xiàn)的位置。 否則它會返回一個布爾值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");
    ?>
    輸出結(jié)果:
    pos1 = (13); type is integer
    pos2 = (0); type is integer
    pos3 = (); type is boolean
    pos3返回的是bool值,即沒有找到子字符串
    希望本文所述對大家的php程序設(shè)計有所幫助。