Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 15009c5cc95cdf00bfa1a55b0263b08b753e51cf @kylebragger committed Oct 13, 2011
Showing with 37 additions and 0 deletions.
  1. +37 −0 parse.php
@@ -0,0 +1,37 @@
+<?php
+
+class URLParse {
+ private static $_re = '@^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?@i';
+ private static $_fieldmap = array(
+ 'scheme' => 2,
+ 'authority' => 4,
+ 'path' => 5,
+ 'query' => 7,
+ 'fragment' => 9
+ );
+
+ public static function parse_url($url)
+ {
+ if (preg_match(self::$_re, $url, $matches))
+ {
+ // map to fields
+ $ret = array();
+ foreach (self::$_fieldmap as $k => $v)
+ {
+ $ret[$k] = isset($matches[$v]) ? $matches[$v] : null;
+ }
+ return $ret;
+ }
+ else
+ {
+ return false;
+ }
+ }
+}
+
+// tests
+print_r(URLParse::parse_url('http://forrst.com')); echo "\n";
+print_r(URLParse::parse_url('http://forrst.com/')); echo "\n";
+print_r(URLParse::parse_url('https://forrst.com/foo#bar')); echo "\n";
+print_r(URLParse::parse_url('http://forrst.com/this.html?what=that')); echo "\n";
+print_r(URLParse::parse_url('http://forrst.com/this.html?foo=that.bar')); echo "\n";

0 comments on commit 15009c5

Please sign in to comment.