PHP Slug Function | YourSite

PHP Slug Function

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*');

🚀 More Blogs You Might Like

Explore more articles and keep learning

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner
Data-Science
Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner...

👁 41 2026-07-26
Read More →
How AI Is Changing the Way Software Is Made
Data-Science
How AI Is Changing the Way Software Is Made

How AI Is Changing the Way Software Is Made...

👁 28 2026-07-26
Read More →
8 Mistakes That Quietly Slow Down Talented Developers
Personal-Development
8 Mistakes That Quietly Slow Down Talented Developers

8 Mistakes That Quietly Slow Down Talented Developers...

👁 65 2026-07-12
Read More →
Does religion teach hatred?
islam
Does religion teach hatred?

It is easy to spread hatred in the name of religion, but understanding the true message of religion is much de...

👁 489 2026-06-30
Read More →