While working with php, at some point we need to convert our strings into slugs for seo friendly urls or some other purpose. And our strings may contain different special characters.
To get rid of that characters and create a slug of that string use this function. It takes the string and return the seo friendly slug. Very simple but efficient.
Call function like this
$slug=slug("This is my title");
This is very helpful when we want to create urls based on some strings like create urls from post title.
To get rid of that characters and create a slug of that string use this function. It takes the string and return the seo friendly slug. Very simple but efficient.
function slug($string){
$string = strtolower(trim($string));
$string = preg_replace('/[^a-z0-9-]/', '-', $string);
$string = preg_replace('/-+/', "-", $string);
return trim($string,"-");
}
Call function like this
$slug=slug("This is my title");
This is very helpful when we want to create urls based on some strings like create urls from post title.
Posting Komentar