2010-12-04 56 views
13

有沒有一種方法可以從解析的URL中反轉網址?PHP parse_url反向 - 解析的網址

$url = 'http://www.domain.com/dir/index.php?query=blabla#more_bla'; 
$parse = parse_url($url); 
print_r($parse); 
/* 
array(
'scheme'=>'http://', 
etc.... 
) 
*/ 
$revere = reverse_url($parse); // probably does not exist but u get the point 

echo $reverse; 
//outputs:// "http://www.domain.com/dir/index.php?query=blabla#more_bla" 

或者,如果有一種方法驗證缺少其推薦的網址的一部分網址,例如

www.mydomain.com

mydomain.com

都應該返回 http://www.mydomain.com 或用正確的子域名

+0

我看看`http_build_url`,但是看起來像這樣一個麻煩,如果網址不同有其他屬性沒有提及'$ url` – Val 2010-12-04 17:52:08

+0

既不是`www.example.com`也不是`example.com`絕對有效網址;將被解釋爲URL路徑。 – Gumbo 2010-12-04 18:07:59

回答

11

你應該可以做

http_build_url($parse) 

根據文檔,它專門設計用於處理來自parse_url的輸出。這兩個函數處理錨點,查詢參數等,所以沒有「其他屬性沒有提及$ url」。

要添加http://時,它的失蹤,解析它之前使用基本檢查:

if (strpos($url, "http://") != 0) 
    $url = "http://$url"; 
+0

我的問題是(也許即時通訊認爲它)但如果`http://`是`https://`,因爲它不是很常見,但電子商務網站有這個功能。我正在開發它作爲一個cms系統,他們需要使用https或其他協議,有時需要使用端口。它會正確處理它嗎? – Val 2010-12-04 18:19:26

5

這個功能應該做的伎倆:

/** 
* @param array $parsed 
* @return string 
*/ 
function unparse_url(array $parsed) { 
    $get = function ($key) use ($parsed) { 
     return isset($parsed[$key]) ? $parsed[$key] : null; 
    }; 

    $pass  = $get('pass'); 
    $user  = $get('user'); 
    $userinfo = $pass !== null ? "$user:$pass" : $user; 
    $port  = $get('port'); 
    $scheme = $get('scheme'); 
    $query  = $get('query'); 
    $fragment = $get('fragment'); 
    $authority = 
     ($userinfo !== null ? "[email protected]" : '') . 
     $get('host') . 
     ($port ? ":$port" : ''); 

    return 
     (strlen($scheme) ? "$scheme:" : '') . 
     (strlen($authority) ? "//$authority" : '') . 
     $get('path') . 
     (strlen($query) ? "?$query" : '') . 
     (strlen($fragment) ? "#$fragment" : ''); 
} 

這裏是它的一個簡短的測試:

function unparse_url_test() { 
    foreach ([ 
     '', 
     'foo', 
     'http://www.google.com/', 
     'http://u:[email protected]:1/path/path?q#frag', 
     'http://u:[email protected]:1/path/path?#', 
     'ssh://[email protected]', 
     '://:@:1/?#', 
     'http://:@foo:1/path/path?#', 
     'http://@foo:1/path/path?#', 
    ] as $url) { 
     $parsed1 = parse_url($url); 
     $parsed2 = parse_url(unparse_url($parsed1)); 

     if ($parsed1 !== $parsed2) { 
      print var_export($parsed1, true) . "\n!==\n" . var_export($parsed2, true) . "\n\n"; 
     } 
    } 
} 

unparse_url_test(); 
12

這些是我用於分解和重建URL的兩個函數:

function http_parse_query($query) { 
    $parameters = array(); 
    $queryParts = explode('&', $query); 
    foreach ($queryParts as $queryPart) { 
     $keyValue = explode('=', $queryPart, 2); 
     $parameters[$keyValue[0]] = $keyValue[1]; 
    } 
    return $parameters; 
} 

function build_url(array $parts) { 
    return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '') . 
     ((isset($parts['user']) || isset($parts['host'])) ? '//' : '') . 
     (isset($parts['user']) ? "{$parts['user']}" : '') . 
     (isset($parts['pass']) ? ":{$parts['pass']}" : '') . 
     (isset($parts['user']) ? '@' : '') . 
     (isset($parts['host']) ? "{$parts['host']}" : '') . 
     (isset($parts['port']) ? ":{$parts['port']}" : '') . 
     (isset($parts['path']) ? "{$parts['path']}" : '') . 
     (isset($parts['query']) ? "?{$parts['query']}" : '') . 
     (isset($parts['fragment']) ? "#{$parts['fragment']}" : ''); 
} 

// Example 
$parts = parse_url($url); 

if (isset($parts['query'])) { 
    $parameters = http_parse_query($parts['query']); 
    foreach ($parameters as $key => $value) { 
     $parameters[$key] = $value; // do stuff with $value 
    } 
    $parts['query'] = http_build_query($parameters); 
} 

$url = build_url($parts); 
0

另一個FPGA實現:

function build_url(array $elements) { 
    $e = $elements; 
    return 
     (isset($e['host']) ? (
      (isset($e['scheme']) ? "$e[scheme]://" : '//') . 
      (isset($e['user']) ? $e['user'] . (isset($e['pass']) ? ":$e[pass]" : '') . '@' : '') . 
      $e['host'] . 
      (isset($e['port']) ? ":$e[port]" : '') 
     ) : '') . 
     (isset($e['path']) ? $e['path'] : '/') . 
     (isset($e['query']) ? '?' . (is_array($e['query']) ? http_build_query($e['query'], '', '&') : $e['query']) : '') . 
     (isset($e['fragment']) ? "#$e[fragment]" : '') 
    ; 
} 

結果應該是:

{ 
    "host": "example.com" 
} 
/* //example.com/ */ 

{ 
    "scheme": "https", 
    "host": "example.com" 
} 
/* https://example.com/ */ 

{ 
    "scheme": "http", 
    "host": "example.com", 
    "port": 8080, 
    "path": "/x/y/z" 
} 
/* http://example.com:8080/x/y/z */ 

{ 
    "scheme": "http", 
    "host": "example.com", 
    "port": 8080, 
    "user": "anonymous", 
    "query": "a=b&c=d", 
    "fragment": "xyz" 
} 
/* http://[email protected]:8080/?a=b&c=d#xyz */ 

{ 
    "scheme": "http", 
    "host": "example.com", 
    "user": "root", 
    "pass": "stupid", 
    "path": "/x/y/z", 
    "query": { 
     "a": "b", 
     "c": "d" 
    } 
} 
/* http://root:[email protected]/x/y/z?a=b&c=d */ 

{ 
    "path": "/x/y/z", 
    "query": "a=b&c=d" 
} 
/* /x/y/z?a=b&c=d */