Very short snippet: PHP slug

Who develops WordPress definitely knows the word slug , usually used to indicate the text strings that contain spaces or other "strange". In practice a URL string Friendly, ie able to be used within a URL. Normally, the conversion of a string in the slug is done automatically by WordPress, as the title of a post, eg "This is a post" is converted to "so-and-a-post". The transformation in slug is similar to that of camelize or camelcase , view here . Here is one possible implementation in PHP:

1
2
3
$s ) { function slug ($ s) {
strtolower ( str_replace ( " " , "-" , preg_replace ( "/[^az A-Z0-9]/" , "" , $s ) ) ) ) ; return ( strtolower ( str_replace ("", "-", preg_replace ("/ [^ az A-Z0-9] /", "", $ s))));
}

10 comments to "Very short snippet: PHP slug"

  1. May 29, 2009 Napolux :

    Among other things your function returns "questounpost" and not "this-a-post" ...

    An alternative to your preg_replace () that does not eliminate the spaces could be

    1
    "/[^a-zA-Z0-9\s]/" , "" , $s ) preg_replace ("/ [^ a-zA-Z0-9 \ s] /", "", $ s)

    (Forgive me, today I am picky)

  2. May 29, 2009 Napolux :

    This, taken from snipplr also remove the double dashes.

    1
    2
    3
    4
    5
    6
    7
    $str ) function slug ($ str)
    {
    strtolower ( trim ( $str ) ) ; $ Str = strtolower ( trim ($ str));
    preg_replace ( '/[^a-z0-9-]/' , '-' , $str ) ; $ Str = preg_replace ('/ [^ a-z0-9-] /', '-', $ str);
    preg_replace ( '/-+/' , "-" , $str ) ; $ Str = preg_replace ('/ - + /', "-", $ str);
    ; return $ str;
    }
  3. May 29, 2009 Napolux :

    I do not see the replacement of accents, as I do not see any special characters like '& etc. ... Or is this just a esempietto?

    I usually use a function similar to str_replace passing () 2 array containing the characters "ugly" and the corresponding "good".

    For example, the apostrophe replace it with "" (empty string), while for example the snail with the dash ...

  4. May 29, 2009 Giovambattista Fazioli :

    @ Napolux: I forgot the space in the filter, I have now corrected. However this is only an example of filter bypass, in the sense that the accented, for example, as remembered you, are not replaced but simply eliminated. : P

  5. May 29, 2009 Giovambattista Fazioli :

    @ Napolux: as well as special characters, of course ... (see ampersen, signs of major / minor, colon, etc ...)

  6. May 29, 2009 Undolog.com "Very short snippet: PHP slug :

    [...] Source: Undolog.com "Very short snippet: PHP slug Related Articles: Very short snippet: PHP isset_post () | [...]

  7. May 29, 2009 IWriteAboutIT :

    Hello Guys,
    Needless to say that just yesterday I was looking for a similar solution.
    Thank you so much for the timing ..
    Tom

  8. May 29, 2009 IWriteAboutIT :

    But if you want a complete and final?

  9. May 29, 2009 Napolux :

    @ IWriteAboutIT:

    But if you want a complete and final?

    Well ... You can filter out all characters except the ones you are interested in saving. You create two arrays with all cases and then go to str_replace ();

  10. March 24, 2011 Giovambattista Fazioli :

    @ IWriteAboutIT:

    But if you want a complete and final?

    You can use the native WordPress:

    1
    2
    / / Insert the string in $ "sentence" to be sanitized
    sanitize_title_with_dashes ( $stringa ) ; $ Slug = sanitize_title_with_dashes ($ string);

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA