使用PHP清理URL

时间:2011-07-04 00:41:52

标签: php url

我正在对网站进行编码,并在网址中包含关键字:

?s=2010%20Federal%20Spending&id=115

标题为“2010年联邦支出”的部分不用于导航;我的网站导航完全忽略了它。我的网站只注意'id',而不是's'。再次,标题是出于SEO的原因。

是否有PHP函数来清理URL的这一部分?例如,将'%20'替换为' - '或类似名称?

4 个答案:

答案 0 :(得分:5)

您需要在mod_rewrite

中查看.htaccess

在.htaccess中添加重写规则很简单。首先,通过将此行添加到.htaccess:

来激活mod_rewrite
RewriteEngine on
RewriteBase /

然后添加您的规则以重定向您的网页:

RewriteRule ^([0-9]+)/([^/]+)$ /yourpage\.php?id=$1&s=$2

这将允许您构建您的网址:

yoursite.com/115/2010-federal-spending

然后,在yourpage.php上:

echo $_GET['id']; // will equal 115 from the above example
echo $_GET['s']; // will equal 2010-federal-spending from the above example

答案 1 :(得分:2)

如果您想要解码网址,请使用urldecode($ your_string)。由于空格不是有效的URL字符,因此在将它用作地址之前,您应该尝试替换标题中的空格。

$mytitle = "2010 Federal Spending";
$fixedtitle = str_replace(" ", "_", $mytitle);
echo $fixedtitle;

您还可以删除可能导致某些问题的其他CHARS,例如“&”

$mytitle = "2010 Federal Spending";
$invchars = array(" ","@",":","/","&");
$fixedtitle = str_replace($invchars, "_", $mytitle);
echo $fixedtitle;

答案 2 :(得分:0)

?s=2010%20Federal%20Spending&id=115

这是一个已编码的网址,空的''已被编码为'%20',您不想替换它,而是先将其解码

$ url = urldecode('?s = 2010%20Federal%20Spending& id = 115')

现在用你喜欢的任何东西替换空字符串

$newUrl = str_replace(' ' ,'-',$url); 
echo urlencode($newUrl);

答案 3 :(得分:0)

您还可以使用here(法语)中描述的功能:

    /**
     * Convert into filename by removing all accents and special characters. Useful for URL Rewriting.
     * @param $text
     * @return string
     */
    public function ConvertIntoFilename($text)
    {
        // Remove all accents.
        $convertedCharacters = array(
            'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A',
            'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a',
            'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O',
            'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o',
            'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E',
            'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
            'Ç' => 'C', 'ç' => 'c',
            'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
            'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
            'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U',
            'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
            'ÿ' => 'y',
            'Ñ' => 'N', 'ñ' => 'n'
        );

        $text = strtr($text, $convertedCharacters);

        // Put the text in lowercase.
        $text = mb_strtolower($text, 'utf-8');

        // Remove all special characters.
        $text = preg_replace('#[^a-z0-9-]#', '-', $text);

        // Remove two consecutive dashes (that's not very pretty).
        $text = preg_replace('/--/U', '-', $text);

        // Remove words containing less than 2 characters (non significant for the meaning)
        $return = array();
        $text = explode('-', $text);

        foreach($text as $word)
        {
            if(mb_strlen($word, 'utf-8') <= 2)   continue;
            $return[] = $word;
        }

        return implode('-', $return);
    }

然而,它仍然需要你修改你的.htaccess,就像AlienWebGuy所提到的那样。 :)