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

问题描述 投票:1回答:1

问题在'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的相关问题

php regex preg-match
1个回答
0
投票

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

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

从手册中引用:

请注意/不是特殊的正则表达式字符。

[...]

分隔符 如果指定了可选的分隔符,它也将被转义。这对于转义PCRE函数所需的分隔符非常有用。 /是最常用的分隔符。

© www.soinside.com 2019 - 2024. All rights reserved.