Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to escape and create a slug [duplicate]

Tags:

php

slug

Possible Duplicate:
URL Friendly Username in PHP?

im somehow confused in using proper functions to escape and create a slug

i used this :

$slug_title = mysql_real_escape_string()($mtitle);

but someone told me not to use it and use urlencode()

which one is better for slugs and security

as i can see in SO , it inserts - between words :

https://stackoverflow.com/questions/941270/validating-a-slug-in-django
like image 720
Mac Taylor Avatar asked Apr 05 '10 19:04

Mac Taylor


1 Answers

Using either MySQL or URL escaping is not the way to go.

Here is an article that does it better:

function toSlug($string,$space="-") {
    if (function_exists('iconv')) {
        $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
    }
    $string = preg_replace("/[^a-zA-Z0-9 -]/", "", $string);
    $string = strtolower($string);
    $string = str_replace(" ", $space, $string);
    return $string;
}

This also works correctly for accented characters.

like image 97
Thomas Avatar answered Sep 27 '22 21:09

Thomas