2012-11-03 43 views
-1

我如何將字符串轉換爲這樣的:轉換網址用PHP

[www.example.com?type=PC&brand=Dell&id=2] 

[www.example.com/PC/Dell/2.html] 

太赫茲對你有所幫助!

+4

**的.htaccess URL重寫**就是答案 –

+0

@ Mr.Alien:他從來沒有說過「重寫」什麼,他說的是「替代」 。 –

+0

@MadaraUchiha我不指望你沒有得到他真正想要的東西,他一定不知道真正的單詞 –

回答

2

在你的.htaccess試試這個

Options +FollowSymLinks 
RewriteEngine on 

RewriteRule /(.*)/(.*)/(.*)\.html www.example.com?type=$1&brand=$2&id=$3 
+0

看到我對這個問題的評論。 OP從來沒有說過重寫任何事情。他想要更換。 –

+1

@MadaraUchiha原來,OP確實希望重寫不是字符串轉換;-) –

+0

確實如此。這意味着OP可以在他的問題上做一些澄清。 –

3

如果你想第一個鏈接映射到第二,使您的網站的網址漂亮,去.htaccessMod_rewrite

這是正確的解決方案:

$str = "[www.example.com?type=PC&brand=Dell&id=2]"; 

$str = trim($str,"[]"); //Remove square brackets, we'll add them back later 
$url = parse_url($str); //Parse the URL into an array 

$query = $url["query"]; //Part after the ? 

$parts = explode("&", $query); //Have each GET variable into an array element 
$parts = array_map(function($e) { 
    return preg_replace("/[^=]+=/", "", $e); 
}, $parts); //Remove the part before the =. 

$parts = implode("/", $parts); //Implode it back into a string. 
$result = $url["path"] . "/" . $parts . ".html"; //Putting it back together 
$result = "[$result]"; //I promised I'll put it back! 

var_dump($result); 
+1

我會upvote這一個,因爲這是根據問題的答案,但OP認爲,否則接受不同。 – itachi