Check if string contains a value in array
The core issue here stems from how in_array functions versus what you are actually trying to achieve. You have an array of allowed domains, but your logic attempts to match the entire user input string against those exact entries. In PHP, in_array($string, $owned_urls) checks if $string is identical to one of the elements in $owned_urls. Since $string is 'my domain name is website3.com' and the array contains just 'website3.com', the check will always fail because the full sentence does not match the short domain name.
To verify whether a string contains at least one value from an array, you need to iterate through the array or use a function designed for partial matches. The most straightforward approach is looping through your whitelist of domains and checking if any of them appear as a substring within the user's input.
Here is a robust implementation using a foreach loop:
$string = 'my domain name is website3.com';
$owned_urls = ['website1.com', 'website2.com', 'website3.com'];
$found = false;
foreach ($owned_urls as $domain) {
// Check if the domain exists within the string
if (strpos($string, $domain) !== false) {
$found = true;
break;
}
}
if ($found) {
echo "Match found";
} else {
echo "Match not found";
}
This code manually iterates through each domain. It uses strpos to find the position of the domain within the string. If the position is not false, a match is detected, and we break out of the loop immediately for efficiency.
Handling Substrings vs. Full Words
While strpos works well for simple cases, it treats the input as raw text. This introduces a potential pitfall: false positives caused by partial matches within words. For instance, if your array contains 'com' and the user inputs 'database.com', strpos will return true. However, you likely only care about top-level domains or specific brand names.
To mitigate this, consider word boundaries or stricter delimiters. You can wrap the domain in a regex pattern that ensures it is not part of a larger word, though for standard TLD checks like .com, checking the position relative to spaces or punctuation is often sufficient. A more rigorous check might look like this:
function containsDomain($string, $allowed_urls) {
foreach ($allowed_urls as $domain) {
// Ensure the domain is surrounded by non-alphanumeric characters
// or marks the start/end of the string
if (preg_match('/(^|[^a-z0-9])' . preg_quote($domain, '/') . '($|[^a-z0-9])/i', $string)) {
return true;
}
}
return false;
}
Using preg_quote is essential here because domains often contain dots (.), which are special characters in regular expressions. Without escaping them, the regex engine would interpret . as "any character," breaking your logic.
Laravel Context
If you are building a modern application using the Laravel framework, this repetitive string manipulation can be handled more elegantly. While Laravel does not have a built-in helper specifically for "array substring search," you can leverage its powerful string and collection utilities.
For example, if you are working with Eloquent models or collections, you might prefer checking against a database column instead of a static array. However, for a simple server-side validation check on a confirmation page, the loop-based approach remains efficient and readable. If you find yourself doing this frequently across multiple routes, consider extracting the logic into a service class or using Laravel's Str facade to keep your controllers clean.
The decision between raw PHP loops and regex depends on your strictness requirements. Simple substring checks are fast but prone to edge cases with partial words; regex adds complexity but offers precision. Always weigh the performance cost of regex against the risk of false positives in your specific use case.
