Reputation: 75
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 »</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
Upvotes: 2
Views: 3132
Reputation: 10874
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(" ", " ", $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>";
Upvotes: 4
Reputation: 50982
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;
}
Upvotes: 1