Question

I am creating search engine friendly slug. Which works fine,but one issue is in it. My code is given below

$s = "This is a - slug";
    function slug($s){
        $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $s);
        return $slug;
        }

This code result in this string This-is-a---slug. But I require This-is-a-slug. Is there any way that when - come with string,then it adjusts correctly.

Était-ce utile?

La solution

Removing the - from the list of accepted characters will yield the desired result:

$slug = preg_replace('/[^A-Za-z0-9]+/', '-', $s);

Returned value: This-is-a-slug

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top