I have a PHP array like so (comes from a MySQL database, YouTube is just an example):
[regexpr] => array(4) (
[0] => (string) http://*.youtube.com/v/*
[1] => (string) https://*.youtube.com/v/*
[2] => (string) http://youtu.be/*
[3] => (string) http://m.youtube.com/watch*
)
Now, what I am trying to achieve is to find any of the matches in a URL (in this case: checking if the URL is a valid YouTube URL).
What I have tried is the following:
for($i = 0; $i < count($regexpr); $i++)
$regexpr[$i] = preg_quote($regexpr[$i]); // because of the URLs
$expr = '/(('. implode('|', $regexpr) . '))/i'; // Concatenating the ORs
$found = preg_match($expr, 'http://www.youtube.com/[foo]&v=MyVidID', $matches);
However, unfortunately, I was unable to successfully concatenate these expressions - I am not getting any successful "hits".
Would anyone have a pointer as to how to solve this issue?
¡Gracias!
Problem is your source "regexs":
http://*.youtube.com/v/*
^--- ^---
the asterisks are "zero or more of the previous" character, not general wildcards. So you're causing the /
chars to be made optional. What you want is probably .*
, which is a "zero or more of ANY character"
However, you're passing those through preg_quote, and that'll escape ANY regex metacharacters, so in effect you're not passing in ANY wildcards. You're passing in literal asterixes, literal periods, etc...
You should escape the regexes yourself and skip the preg_quote stage, e.g.
http:\/\/.*\.youtube.com\/v\/.*
what if you parse the urls and then compare them to a white list? Something like:
$myurls= array('http://example.com', 'http://foo.com/', 'http://mysite.com/', 'http://youtube.com?feeble=2&blob=ixls');
$whitelist=array('foo.com', 'youtube.com');
foreach($myurls as $url){
$parsed=parse_url($url);
if(in_array($parsed['host'],$whitelist)) echo "$url OK";
}