成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

PHP中startsWith()和endsWith()函數(shù)如何使用

PHP中startsWith()和endsWith()函數(shù)如何使用,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

為淮北等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及淮北網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設計、成都做網(wǎng)站、淮北網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

例如:

$str = '|apples}';echo startsWith($str, '|'); //Returns trueecho endsWith($str, '}'); //Returns true

上面的正則表達式功能,但上面提到的其他調整:

 function startsWith($needle, $haystack) {     return preg_match('/^' . preg_quote($needle, '/') . '/', $haystack);
 } function endsWith($needle, $haystack) {     return preg_match('/' . preg_quote($needle, '/') . '$/', $haystack);
 }

簡而言之:

function startsWith($str, $needle){   return substr($str, 0, strlen($needle)) === $needle;
}function endsWith($str, $needle){
   $length = strlen($needle);   return !$length || substr($str, - $length) === $needle;
}

您可以使用substr_compare函數(shù)來檢查start-with和ends-with:

function startsWith($haystack, $needle) {    return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
}function endsWith($haystack, $needle) {    return substr_compare($haystack, $needle, -strlen($needle)) === 0;
}

這應該是PHP 7(  基準腳本 )上最快的解決方案之一。 測試了8KB干草堆,各種長度的針和完整,部分和無匹配的情況。 strncmp是一個更快的觸摸開始 - 但它無法檢查結束。

為什么不以下?

//How to check if a string begins with another string$haystack = "valuehaystack";
$needle = "value";if (strpos($haystack, $needle) === 0){    echo "Found " . $needle . " at the beginning of " . $haystack . "!";
}

輸出:

在valuehaystack開頭找到價值!

請記住,如果在大海撈針中找不到針, strpos將返回false,并且當且僅當在指數(shù)0處找到針時才會返回0(AKA開頭)。

以下是:

$haystack = "valuehaystack";
$needle = "haystack";//If index of the needle plus the length of the needle is the same length as the entire haystack.if (strpos($haystack, $needle) + strlen($needle) === strlen($haystack)){    echo "Found " . $needle . " at the end of " . $haystack . "!";
}

在這種情況下,不需要函數(shù)startsWith()as

(strpos($stringToSearch, $doesItStartWithThis) === 0)

將準確地返回真或假。

這看起來很奇怪,所有狂野的功能在這里都很猖獗。

我意識到這已經(jīng)完成了,但你可能想看一下 strncmp因為它允許你把字符串的長度進行比較,所以:

function startsWith($haystack, $needle, $case=true) {    if ($case)        return strncasecmp($haystack, $needle, strlen($needle)) == 0;    else
        return strncmp($haystack, $needle, strlen($needle)) == 0;
}

看完上述內容,你們掌握PHP中startsWith()和endsWith()函數(shù)如何使用的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

新聞標題:PHP中startsWith()和endsWith()函數(shù)如何使用
網(wǎng)站鏈接:http://jinyejixie.com/article4/gphgie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈、建站公司移動網(wǎng)站建設、搜索引擎優(yōu)化、定制開發(fā)、網(wǎng)站導航

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司
闵行区| 交口县| 兴化市| 蒙山县| 新宾| 林芝县| 清河县| 安阳市| 娄底市| 丹棱县| 娄底市| 蓬莱市| 四平市| 阿勒泰市| 阿勒泰市| 紫阳县| 辉县市| 鄄城县| 宣化县| 射洪县| 阜平县| 龙山县| 固原市| 义马市| 柞水县| 通榆县| 红安县| 余庆县| 阿瓦提县| 和龙市| 象州县| 墨脱县| 顺平县| 兰溪市| 莱西市| 大田县| 莒南县| 温州市| 全椒县| 分宜县| 桓仁|