PHP - Déterminer si une URL est ou non absolue

Avec cette simple fonction, vous pourrez tester si l'URL passée en paramètre est, ou non, absolue :

public function is_absolute($url)
{
    $pattern = "/^(?:ftp|https?|feed):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*
    (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:
    (?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?]
    (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/xi";

    return (bool) preg_match($pattern, $url);
}

Remarque : Cette fonction est notamment utilisée par le CMS Drupal.


28 mai 2018 - Wakonda - PHP

Rechercher

Tags

Publicité

Suivez-nous

Aidez-nous !


Loading…
Loading the web debug toolbar…
Attempt #