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

Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 19 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 25 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 46 2026-05-09
Read More →
Popular IT Sector Buzzwords You Should Know in 2026
skill
Popular IT Sector Buzzwords You Should Know in 2026

Popular IT Sector Buzzwords You Should Know in 2026...

👁 54 2026-05-07
Read More →