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>
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!