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

Patricia Arquette
Release: 2024-10-24 19:11:29
Original
234 people have browsed it

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

Check String for URL Match in Array

To detect whether a string contains a URL stored in an array, you can use a PHP loop to compare the string to each URL in the array. Here's a corrected version of your code:

<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>
Copy after login

This code loops through the URLs, and for each URL, uses the strpos() function to check if the string contains the URL. If a match is found, the code outputs "Match found" and returns true. If no match is found, it outputs "Match not found" and returns false.

The above is the detailed content of How to Check if a String Contains a URL from an Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!