Below is the function to convert string to slug.
function createSlug($str, $delimiter = '-'){
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}
For example:
$str= 'this is slug'; //If this is the string which we have to convert as slug
$createdSlug= createSlug($str); //Call funtion
echo $createdSlug; // Print output
Output:
this-is-slug
No comments:
Post a Comment