Skip to content

Instantly share code, notes, and snippets.

Created October 26, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/34cfaea1cf71dcc5dc5224612b6d63ab to your computer and use it in GitHub Desktop.
Save anonymous/34cfaea1cf71dcc5dc5224612b6d63ab to your computer and use it in GitHub Desktop.
regex: detect internal domains in urls
<?php
$allow = array
(
'*.eitb.*',
'tortolika'
);
$urls = array
(
'www.eitb.eus/asjdhjashdjahjkdhajkhdjk/',
'http://www.eitb.eus/asjdhjashdjahjkdhajkhdjk/',
'http://pre.eitb.eus/',
'http://tortolika/',
'tortolika/adasdasdass',
'http://www.google.es',
'www.google.es'
);
$z = array();
foreach($urls as $url)
{
foreach($allow as $a)
{
$b = preg_replace("/\./", '\.', $a);
$b = preg_replace("/\*/", '[^\s]+', $a);
$b = "^(https?:\/\/|){$b}";
if(preg_match("/$b/i", $url))
$z[] = $url;
}
}
print_r($z);
@ZiTAL
Copy link

ZiTAL commented Oct 26, 2017

Output:
Array
(
[0] => www.eitb.eus/asjdhjashdjahjkdhajkhdjk/
[1] => http://www.eitb.eus/asjdhjashdjahjkdhajkhdjk/
[2] => http://pre.eitb.eus/
[3] => http://tortolika/
[4] => tortolika/adasdasdass
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment