在 Laravel 中使用阿拉伯字符作为 Slug

Mom*_*mad 3 php regex seo slug laravel

我正在使用这个包Github:eluquent-sluggable-persian使用阿拉伯语来表示 slug,他们使用正则表达式来表示包配置文件,我不太擅长它,我遇到的错误是

sluggable.php 第 106 行中的 ErrorException:
preg_replace():编译失败:在偏移量 0 处无需重复

sluggable.php 是我应该用原始配置文件替换的配置文件,我需要知道如何解决这个问题。

有错误的代码

'method' => function($string, $separator = '-')
{
    $_transliteration = array(
        '/ä|æ|?/' => 'ae',
        '/ö|œ/' => 'oe',
        '/ü/' => 'ue',
        '/Ä/' => 'Ae',
        '/Ü/' => 'Ue',
        '/Ö/' => 'Oe',
        '/À|Á|Â|Ã|Å|?|?|?|?|?/' => 'A',
        '/à|á|â|ã|å|?|?|?|?|?|ª/' => 'a',
        '/Ç|?|?|?|?/' => 'C',
        '/ç|?|?|?|?/' => 'c',
        '/Ð|?|?/' => 'D',
        '/ð|?|?/' => 'd',
        '/È|É|Ê|Ë|?|?|?|?|?/' => 'E',
        '/è|é|ê|ë|?|?|?|?|?/' => 'e',
        '/?|?|?|?/' => 'G',
        '/?|?|?|?/' => 'g',
        '/?|?/' => 'H',
        '/?|?/' => 'h',
        '/Ì|Í|Î|Ï|?|?|?|?|?|?/' => 'I',
        '/ì|í|î|ï|?|?|?|?|?|?/' => 'i',
        '/?/' => 'J',
        '/?/' => 'j',
        '/?/' => 'K',
        '/?/' => 'k',
        '/?|?|?|?|?/' => 'L',
        '/?|?|?|?|?/' => 'l',
        '/Ñ|?|?|?/' => 'N',
        '/ñ|?|?|?|?/' => 'n',
        '/Ò|Ó|Ô|Õ|?|?|?|?|?|Ø|?/' => 'O',
        '/ò|ó|ô|õ|?|?|?|?|?|ø|?|º/' => 'o',
        '/?|?|?/' => 'R',
        '/?|?|?/' => 'r',
        '/?|?|?|?|Š/' => 'S',
        '/?|?|?|?|š|?/' => 's',
        '/?|?|?|?/' => 'T',
        '/?|?|?|?/' => 't',
        '/Ù|Ú|Û|?|?|?|?|?|?|?|?|?|?|?|?/' => 'U',
        '/ù|ú|û|?|?|?|?|?|?|?|?|?|?|?|?/' => 'u',
        '/Ý|Ÿ|?/' => 'Y',
        '/ý|ÿ|?/' => 'y',
        '/?/' => 'W',
        '/?/' => 'w',
        '/?|?|Ž/' => 'Z',
        '/?|?|ž/' => 'z',
        '/Æ|?/' => 'AE',
        '/ß/' => 'ss',
        '/?/' => 'IJ',
        '/?/' => 'ij',
        '/Œ/' => 'OE',
        '/ƒ/' => 'f'
    );
    $quotedReplacement = preg_quote($separator, '/');
    $merge = array(
        '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
        '/[\s\p{Zs}]+/mu' => $separator,
        sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
    );
    $map = $_transliteration + $merge;
    unset($_transliteration);
    return preg_replace(array_keys($map), array_values($map), $string);
}
Run Code Online (Sandbox Code Playgroud)

Mom*_*mad 7

我已经解决了这个问题,已经用另一种方法替换了这个方法

'method' => function ($string, $separator = '-') {
    if (is_null($string)) {
        return "";
    }

    // Remove spaces from the beginning and from the end of the string
    $string = trim($string);

    // Lower case everything
    // using mb_strtolower() function is important for non-Latin UTF-8 string | more info: https://www.php.net/manual/en/function.mb-strtolower.php
    $string = mb_strtolower($string, "UTF-8");;

    // Make alphanumeric (removes all other characters)
    // this makes the string safe especially when used as a part of a URL
    // this keeps latin characters and arabic charactrs as well
    $string = preg_replace("/[^a-z0-9_\s\-????????????????????????????????????]#u/", "", $string);

    // Remove multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);

    // Convert whitespaces and underscore to the given separator
    $string = preg_replace("/[\s_]/", $separator, $string);

    return $string;
},
Run Code Online (Sandbox Code Playgroud)

它现在工作谢谢^^