PHP Slug Function | YourSite

PHP Slug Function

Web Development • 515 views

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

Summer Hydration Hacks: Staying Cool from the Inside Out
health
Summer Hydration Hacks: Staying Cool from the Inside Out

Summer Hydration Hacks: Staying Cool from the Inside Out...

👁 27 2026-04-08
Read More →
Earthquake Activity Near Kolkata, India — Latest Update (27 Feb 2026)
miscellaneous
Earthquake Activity Near Kolkata, India — Latest Update (27 Feb 2026)

Earthquake Activity Near Kolkata, India — Latest Update (27 Feb 2026)...

👁 99 2026-02-27
Read More →
আসক্তির শৃঙ্খলে আটকে যাওয়া: ডিজিটাল আসক্তির বাস্তব চিত্র
youth-society
আসক্তির শৃঙ্খলে আটকে যাওয়া: ডিজিটাল আসক্তির বাস্তব চিত্র

আসক্তির শৃঙ্খলে আটকে যাওয়া: ডিজিটাল আসক্�...

👁 123 2025-12-29
Read More →
How Many Emotional States Can a Human Experience?
life-lesson
How Many Emotional States Can a Human Experience?

How Many Emotional States Can a Human Experience?...

👁 169 2025-12-14
Read More →