· 1 min read

How to Slugify a String in PHP

Here is a snippet for generating a URL friendly slug in PHP

function slugify($string){
        return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string), '-'));
    }

And you can use it in your code like so

<?php echo slugify('!hello world!? how are you???'); // => hello-world-how-are-you ?>

2 comments

  • jeeturay2

    if we include hindi character like धो,नी के बेड़े में सेना की यह दमदार गाड़

    then how to create slug

  • Reader

Leave a comment