PHP function to generate a slug for SEO URLs

Increase SEO by using your article name/product name/category name/whatever in your URLs instead of a non-descriptive database ID.

function slug($string) {
    $string = trim($string);
    $string = strtolower($string);
    $string = str_replace('&', 'and', $string);
    $string = preg_replace('/[^a-z0-9-]/', '-', $string);
    $string = preg_replace('/-+/', "-", $string);
    return $string;
}
  • Share/Bookmark

Related posts:

  1. MySQL string escaping in PHP

Leave a Reply