A simple WordPress style slug generator function added to litwicki/common

/**
 * @param $string
 * @param null $id
 *
 * @throws \Exception
 */
public static function createUrlSlug($string, $id = null)
{
    try {
        $slug = utf8_encode($string);
        $slug = str_replace(' ', '-', $slug);
        $slug = strtolower($slug);
        $slug = preg_replace('/[^\da-z]/i', '', $slug);
        $slug = sprintf('%s-%s', $id, $slug);
        return urlencode($slug);
    }
    catch(\Exception $e) {
        throw $e;
    }
}

 

Categories: Random

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *