55

validateurl

validate_url
SYNOPSIS

validate_url($url)

DESCRIPTION
CODE
  1. function validate_url($url) {
  2.     return filter_var($url, FILTER_VALIDATE_URL);
  3. }
prefix_url
SYNOPSIS

prefix_url($url, $protocol='http')

DESCRIPTION
CODE
  1. function prefix_url($url, $protocol='http') {
  2.     if (!@parse_url($url, PHP_URL_SCHEME)) {
  3.         $url = $protocol . '://' . $url;
  4.     }
  5.  
  6.     return $url;
  7. }
normalize_url
SYNOPSIS

normalize_url($url)

DESCRIPTION
CODE
  1. function normalize_url($url) {
  2.     $purl = @parse_url($url);
  3.  
  4.     if (!$purl) {
  5.         return false;
  6.     }
  7.  
  8.     $scheme=$host=$port=$user=$pass=$path=$query=$fragment=false;
  9.  
  10.     extract($purl);
  11.  
  12.     if ($scheme) {
  13.         $scheme = strtolower($scheme);
  14.     }
  15.  
  16.     if ($host) {
  17.         $host = strtolower($host);
  18.     }
  19.  
  20.     if ($port and $port == getservbyname($scheme, 'tcp')) {
  21.         $port = false;
  22.     }
  23.  
  24.     foreach (array('user', 'pass', 'host', 'path') as $p) {
  25.         if ($$p) {
  26.             $$p = preg_replace_callback('/%[0-9a-f]{2}/i', function($m) { return strtoupper($m[0]); }, $$p);
  27.         }
  28.     }
  29.  
  30.     if ($path) {
  31.         $path = _restore_allowed_chars(_remove_dot_segments($path));
  32.     }
  33.  
  34.     if ($host && !$path) {
  35.         $path = '/';
  36.     }
  37.  
  38.     $newurl = $scheme . '://';
  39.  
  40.     if ($host) {
  41.         if ($user) {
  42.             $newurl .= $user;
  43.             if ($pass) {
  44.                 $newurl .= ':' . $pass;
  45.             }
  46.             $newurl .= '@';
  47.         }
  48.  
  49.         $newurl .= $host;
  50.  
  51.         if ($port) {
  52.             $newurl .= ':' . $port;
  53.         }
  54.     }
  55.  
  56.     $newurl .= $path;
  57.  
  58.     if ($query) {
  59.         $q = array();
  60.         foreach (explode('&', $query) as $s) {
  61.             if ($s and preg_match('/[^=]+=[^=]+/', $s)) {
  62.                 $q[] = $s;
  63.             }
  64.         }
  65.         if ($q) {
  66.             sort($q);
  67.             $newurl .= '?' . implode('&', $q);
  68.         }
  69.     }
  70.  
  71.     if ($fragment) {
  72.         $newurl .= '#' . $fragment;
  73.     }
  74.  
  75.     return $newurl;
  76. }
  1. function _remove_dot_segments($path) {
  2.     $newpath = '';
  3.  
  4.     $watchdog = 100;
  5.  
  6.     while ($path && $watchdog-- > 0) {
  7.         if (substr($path, 0, 2) == './') {
  8.             $path = substr($path, 2);
  9.         }
  10.         elseif (substr($path, 0, 3) == '../') {
  11.             $path = substr($path, 3);
  12.         }
  13.         elseif (substr($path, 0, 3) == '/./' || $path == '/.') {
  14.             $path = '/' . substr($path, 3);
  15.         }
  16.         elseif (substr($path, 0, 4) == '/../' || $path == '/..') {
  17.             $path   = '/' . substr($path, 4);
  18.             $i    = strrpos($newpath, '/');
  19.             $newpath = $i === false ? '' : substr($newpath, 0, $i);
  20.         }
  21.         elseif ($path == '.' || $path == '..') {
  22.             $path = '';
  23.         }
  24.         else {
  25.             $i = strpos($path, '/');
  26.             if ($i === 0) {
  27.                 $i = strpos($path, '/', 1);
  28.             }
  29.             if ($i === false) {
  30.                 $i = strlen($path);
  31.             }
  32.             $newpath .= substr($path, 0, $i);
  33.             $path = substr($path, $i);
  34.         }
  35.     }
  36.  
  37.     return $newpath;
  38. }
  1. function _restore_allowed_chars($s) {
  2.     $from = array(
  3.         '%41', '%42', '%43', '%44', '%45', '%46', '%47', '%48', '%49', '%4A', '%4B', '%4C', '%4D', '%4E', '%4F', '%50', '%51', '%52', '%53', '%54', '%55', '%56', '%57', '%58', '%59', '%5A',
  4.         '%61', '%62', '%63', '%64', '%65', '%66', '%67', '%68', '%69', '%6A', '%6B', '%6C', '%6D', '%6E', '%6F', '%70', '%71', '%72', '%73', '%74', '%75', '%76', '%77', '%78', '%79', '%7A',
  5.         '%30', '%31', '%32', '%33', '%34', '%35', '%36', '%37', '%38', '%39',
  6.         '%2D', '%2E', '%5F', '%7E'
  7.     );
  8.  
  9.     $to = array(
  10.         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  11.         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  12.         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  13.         '-', '.', '_', '~'
  14.     );
  15.  
  16.     return str_replace($from, $to, $s);
  17. }

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].