-1

如何转换这样的字符串:

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

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

谢谢你的帮助!

4

2 回答 2

3

如果您想将第一个链接映射到第二个链接,以使您网站上的 URL 更漂亮,请使用.htaccessand Mod_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);
于 2012-11-03T13:58:58.253 回答
2

在你的 .htaccess 中试试这个

Options +FollowSymLinks
RewriteEngine on

RewriteRule /(.*)/(.*)/(.*)\.html www.example.com?type=$1&brand=$2&id=$3
于 2012-11-03T13:53:05.843 回答