Question

I am writing a custom blog script and as part of it am using a slug for post URLs. The code I have for it so far is

public function get_slug($str) {
    // convert to lowercase
    $str = strtolower($str);
    // remove special chars
    $str = preg_replace('/[^a-zA-Z0-9]/i', ' ', $str);
    // remove what space from beginning and end
    $str = trim($str);
    // remove repeat spaces
    $str = preg_replace('/\s+/', ' ', $str);
    // replace spaces with hyphens
    $str = preg_replace('/\s+/', '-', $str);

    return $str;
}

And this works great for most parts, however I have noticed if there is a special char in the middle of the word it simply replaces it with a hyphen turning 'can't' into 'can-t' instead of 'cant'

Whilst at preset I can just edit the db and fix it by hand, I would like to have it automatically just strip special chars from the middle of words and not replace them with a hypen.

Was it helpful?

Solution

Try changing:

$str = preg_replace('/[^a-zA-Z0-9]/i', ' ', $str);

to:

$str = preg_replace('/[^a-z0-9\s]/i', '', $str);

This will not put spaces where you don't want them but allow the ones that are already there to stay (for now). Then you won't have spaces in the middle of words. (I also took out the A-Z from your regex since it is not necessary since the string is already lowercase and you are also using the i modifier which makes it case insensitive anyway).

OTHER TIPS

You're replacing the ' here:

$str = preg_replace('/[^a-zA-Z0-9]/i', ' ', $str);

Put this before the above:

$str = preg_replace("/'/i", '', $str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top