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)))); } |










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
(Forgive me, today I am picky)
This, taken from snipplr also remove the double dashes.
2
3
4
5
6
7
{
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;
}
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 ...
@ 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.
@ Napolux: as well as special characters, of course ... (see ampersen, signs of major / minor, colon, etc ...)
[...] Source: Undolog.com "Very short snippet: PHP slug Related Articles: Very short snippet: PHP isset_post () | [...]
Hello Guys,
Needless to say that just yesterday I was looking for a similar solution.
Thank you so much for the timing ..
Tom
But if you want a complete and final?
@ IWriteAboutIT:
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 ();
@ IWriteAboutIT:
You can use the native WordPress:
2
sanitize_title_with_dashes ( $stringa ) ; $ Slug = sanitize_title_with_dashes ($ string);