Skip to content

Instantly share code, notes, and snippets.

/regex.php
Created Oct 26, 2017

Embed
What would you like to do?
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

This comment has been minimized.

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
You can’t perform that action at this time.