如何檢查字串是否包含特定子字串?
要檢查字串是否包含特定子字串,可以使用“strlen”和“strpos”函式。“strlen”給出了字串的整個長度。函式“strpos”查詢子字串在字串中首次出現的位置。
示例
<?php
$str_1 = "thisisasample";
$str_2 = "asample";
if (strpos($str_1, $str_2) >= 0 && strpos($str_1, $str_2) < strlen($str_1))
echo("The substring is present within the string.");
else
echo("The substring is not present within the string.");
?>輸出
The substring is present within the string.
定義了兩個字串,並藉助“strpos”函式檢查第二個字串在第一個字串中的位置並與第一個字串的長度進行比較。相關訊息顯示在控制檯上。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP