Page 1 of 1

convert to sef url function

Posted: Fri Jan 02, 2009 12:11 pm
by php4newbies
I have framework I created and I've created a few 'helper' functions. One function is converting a user entered title (actually a blog post title) I am storing the title and also storing a 'seo_title' the blog post is pulled based on the url entered.
So, when some goes to mysite com/this-is-a-sef-url it uses 'this-is-a-sef-url' to get that blog post.
Anyway here is the function I use to convert:

Code: Select all

function convert_to_sef_url($str)
{
    $str = str_replace("'","",$str); //replace ' with nothing
    $str = str_replace('"',"",$str); //replace " with nothing
    $str = preg_replace("/[^a-zA-Z0-9_-]/","-", $str); //convert non alphanumeric and non - _ to -
    $str = preg_replace ( "/-+/" , "-" , $str ); //convert multiple dashes to a single dash
    $str = strtolower($str);
    return $str;
}
It works great. can anyone think of a more efficient way or find any bugs?
I want to make sure only a-z and A-Z and 0-9 and underscores and dashes are the only things allowed in the sef url and no multiple dashes. I think I've got everything but a fresh pair of eyes is always nice.

Re: convert to sef url function

Posted: Fri Jan 02, 2009 2:30 pm
by Syntac
Well, since you replace all invalid characters not in the whitelist with "-", I'd say you've got everything.

Although SEF is a bit of a misnomer here. All proper search engines can handle query strings. Do you mean UEF (User Eye Friendly), maybe? :)

Re: convert to sef url function

Posted: Fri Jan 02, 2009 3:59 pm
by php4newbies
Syntac wrote:Well, since you replace all invalid characters not in the whitelist with "-", I'd say you've got everything.

Although SEF is a bit of a misnomer here. All proper search engines can handle query strings. Do you mean UEF (User Eye Friendly), maybe? :)
Yes your probably right I never was good with naming conventions - I once got reprimanded at work for having a table named promo_or_setup_user_code_usage_assoc_table was told to use discount_use_table instead :)