문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top