If you are working with PHP and developing portal like any CMS, you
must need user friednly URL and for that you have to create dynamic auto
generated URL. A slug is a part of the URL to access the content. Here I will let you know how to create URL slug from post title.
You can replace spaces with dashes later to make it more like a slug. As you can see this isn’t the most user friendly URL so you need to be able to replace the spaces with hyphens. This is a really basic question – but what is meant by the term ‘category slug‘ when … See, now, I was thinking how cool it would be if I could use a tag like, regular expression function that replaces spaces between words with hyphens.
function create_slug($string){
$myslug=preg_replace(‘/[^A-Za-z0-9-]+/’, ‘-‘, $string);
return $myslug;
}
echo create_slug(‘This is working good’);
//returns ‘this-is-working-good’
?>
Read other PHP / WordPress articles.
You can replace spaces with dashes later to make it more like a slug. As you can see this isn’t the most user friendly URL so you need to be able to replace the spaces with hyphens. This is a really basic question – but what is meant by the term ‘category slug‘ when … See, now, I was thinking how cool it would be if I could use a tag like, regular expression function that replaces spaces between words with hyphens.
Code for How to Create URL Slug from Post Title
<?phpfunction create_slug($string){
$myslug=preg_replace(‘/[^A-Za-z0-9-]+/’, ‘-‘, $string);
return $myslug;
}
echo create_slug(‘This is working good’);
//returns ‘this-is-working-good’
?>
Read other PHP / WordPress articles.

No comments:
Post a Comment