Last active
August 29, 2015 14:13
-
-
Save damirm/a91305159ba3142d253d to your computer and use it in GitHub Desktop.
php parse links test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function prepareLink($link, $allowedDomains = []) { | |
$parsed = parse_url($link); | |
$withoutScheme = preg_replace("/[a-z]+:\/\/(.*)/", "$1", $link); | |
if (in_array($parsed['host'], $allowedDomains)) { | |
$href = preg_replace(sprintf("/.+%s(.*)/", $parsed['host']), "$1", $link); | |
return sprintf('<a href="%s">%s</a>', $href, $withoutScheme); | |
} | |
return $withoutScheme; | |
} | |
$allowedDomains = ['site.ru']; | |
$links = [ | |
'http://site.ru/eee.php', | |
'http://test.ru/eee.php' | |
]; | |
foreach ($links as $link) { | |
var_dump(prepareLink($link, $allowedDomains)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment