如何在 PHP 中檢查字串是否包含數組中的 URL?

Patricia Arquette
發布: 2024-10-24 19:11:29
原創
234 人瀏覽過

How to Check if a String Contains a URL from an Array in PHP?

檢查陣列中的字串是否符合

要偵測字串是否包含儲存在陣列中的URL,可以使用PHP 循環將字串與數組中的每個URL 進行比較。以下是程式碼的修正版本:

<code class="php">$owned_urls = ['website1.com', 'website2.com', 'website3.com'];
$string = 'my domain name is website3.com';

foreach ($owned_urls as $url) {
    // Use strpos() to check for matches (case-sensitive)
    if (strpos($string, $url) !== FALSE) {
        echo "Match found"; 
        return true;
    }
}

// If no match is found, return false
echo "Match not found";
return false;</code>
登入後複製

此程式碼循環遍歷 URL,對於每個 URL,使用 strpos() 函數檢查字串是否包含該 URL。如果找到匹配項,程式碼將輸出「Match found」並傳回 true。如果未找到匹配,則輸出「未找到匹配」並傳回 false。

以上是如何在 PHP 中檢查字串是否包含數組中的 URL?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!