PHP Slug Function

Rumman Ansari   2022-12-06   Developer   web development > PHP Slug Function   336 Share

In this blog you will learn how to create a PHP Slug Function and How to use it.

Code


// functions body
function php_slug($string)  
 {  
      $slug = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($string))); 
      $final_slug = preg_replace('/-+/', '-', $slug); 
      return $final_slug;  
 }
 

How to call it

You can send a variable like below $blogTitle


$post_url = php_slug($blogTitle);

You can send a POST variable like below $_POST['BlogStatus']


$post_url = php_slug($_POST['BlogStatus']);

You can send a string like below 'Hello%-world*'


$post_url = php_slug('Hello%-world*');