如何修复 preg_match() 未知修饰符 '?'在 localeconv() 中

how to fix preg_match() unknown modifier '?' in localeconv()

问题在'decimal_point' => string '/'

但是有人有好的解决方案吗?

错误:

  preg_match() unknown modifier '?'

函数:

     * @param   string  $str    input string
     * @return  boolean
     */
    public static function numeric($str)
    {
        // Get the decimal point for the current locale
        list($decimal) = array_values(localeconv());

        // A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)

         return (bool) preg_match('/^-?+(?=.*[0-9])[09]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str); 
}

这是 localeconv() 转储:

array (size=18)
  'decimal_point' => string '/' (length=1)
  'thousands_sep' => string ',' (length=1)
  'int_curr_symbol' => string 'IRR' (length=3)
  'currency_symbol' => string 'ريال' (length=8)
  'mon_decimal_point' => string '/' (length=1)
  'mon_thousands_sep' => string ',' (length=1)
  'positive_sign' => string '' (length=0)
  'negative_sign' => string '-' (length=1)
  'int_frac_digits' => int 2
  'frac_digits' => int 2
  'p_cs_precedes' => int 0
  'p_sep_by_space' => int 0
  'n_cs_precedes' => int 0
  'n_sep_by_space' => int 0
  'p_sign_posn' => int 3
  'n_sign_posn' => int 3
  'grouping' => 
    array (size=1)
      0 => int 3
  'mon_grouping' => 
    array (size=1)
      0 => int 3

github 上的相关问题 koseven/issues #351

由于您在正则表达式中使用 / 作为分隔符,因此也将其作为第二个参数传递给 preg_quote 函数:

return (bool) preg_match('/^-?+(?=.*[0-9])[09]*+' . preg_quote($decimal, '/') . '?+[0-9]*+$/D', (string) $str);
// -----------------------------------------------------------------------^

引自手册:

Note that / is not a special regular expression character.

[...]

delimiter
If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required by the PCRE functions. The / is the most commonly used delimiter.