php startswith

/**
 * StartsWith
 * Tests if a text starts with an given string.
 *
 * @param   $Haystack  string 
 * @param   $Needle  string
 * @return    bool
 */
function StartsWith($Haystack, $Needle){
    // Recommended version, using strpos
    return strpos($Haystack, $Needle) === 0;
}
 
// Another way, using substr
function StartsWithOld($Haystack, $Needle){
    return substr($Haystack, 0, strlen($Needle)) == $Needle;
}

 

 

strpos 用法

<?php
echo strpos("Hello world!","wo");
?>

输出:

6

你可能感兴趣的:(PHP)