用php转换URL

时间:2012-11-03 13:48:26

标签: php url-rewriting

如何转换这样的字符串:

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

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

这是你的帮助!

2 个答案:

答案 0 :(得分: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 :(得分:2)

在你的.htaccess中试试这个

Options +FollowSymLinks
RewriteEngine on

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