简体   繁体   中英

Sanitizing urls using php for seo friendly

I am in the process of turning my urls seo friendly.

My urls for my blog currently look like:

http://domain.com/news/view-article.php?id=23+category=qrops+title=moving-your-pension-abroad---what-are-the-benefits?

How can I ensure characters like @ ? >< don't appear in my url?

How can I avoid duplicate --- ?

Code to generate the url is as follows:

<a class="small magenta awesome" title="View full article" href="view-article.php?id='.$row['id'].'+category='.strtolower($row['category']).'+title='.strtolower(str_replace(" ","-",$row['title'])).'">View full article &raquo;</a>

Pretty sure I am doing something wrong but I'm trying...

Help appreciated..

I will move on to using the mod_rewrite in apache afterwards

I used to use this function

function SEO($input){ 
    //SEO - friendly URL String Converter    
    //ex) this is an example -> this-is-an-example
    $input = str_replace("&nbsp;", " ", $input);
    $input = str_replace(array("'", "-"), "", $input); //remove single quote and dash
    $input = mb_convert_case($input, MB_CASE_LOWER, "UTF-8"); //convert to lowercase
    $input = preg_replace("#[^a-zA-Z]+#", "-", $input); //replace everything non an with dashes
    $input = preg_replace("#(-){2,}#", "$1", $input); //replace multiple dashes with one
    $input = trim($input, "-"); //trim dashes from beginning and end of string if any
    return $input; 
}

For an example, you can use this by

echo "<title>".SEO($title)."</title>";

I use this sweet function to generate SEO friendly URL

function url($url) {
        $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
        $url = trim($url, "-");
        $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
        $url = strtolower($url);
        $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
        return $url;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM