Вопрос

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