Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP slugify method without using preg_replace()

Tags:

php

pcre

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';
    }
    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.

like image 447
mushfiq Avatar asked Nov 27 '25 02:11

mushfiq


1 Answers

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".

like image 82
Iiridayn Avatar answered Nov 28 '25 15:11

Iiridayn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!