开发者

PHP slugify method without using preg_replace()

I am using following slugify method,in my local dev its working fine,but in my production server(CentOs) and the PCRE UTF8 supported but "No Unicode properties support".

function slugify($text)
{
    // replace non letter or digits by -
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

    // trim
    $text = trim($text, '-');

    // transliterate
    if (function_exists('iconv')) {
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    }

    // lowercase
    $text = strtolower($text);

    // remove unwanted characters
    $text = preg_replace('~[^-\w]+~', '', $text);

    if (empty($text)) {
        return 'n-a开发者_Go百科';
    }
    return $text;
}

And preg_replace is not working there,is there any method that can work as preg_replace,or any slugify muthod that can work as the above function.

Thanks in advance.


This sounds like the same issue described here: http://chrisjean.com/2009/01/31/unicode-support-on-centos-52-with-php-and-pcre/. I've run into it before myself, and that link is how I fixed it (or rather, how our sysadmin fixed it).

Essentially, the \pL in the first regex will not run or compile if you do not have "Unicode properties support".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜