Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preg_match() unkown modifier '?' in valid.php . #351

Closed
masthisis opened this issue Apr 4, 2019 · 11 comments
Closed

preg_match() unkown modifier '?' in valid.php . #351

masthisis opened this issue Apr 4, 2019 · 11 comments
Labels
bug Verified Bug
Milestone

Comments

@masthisis
Copy link

/**
* Checks whether a string is a valid number (negative and decimal numbers allowed).
*
* Uses {@link http://www.php.net/manual/en/function.localeconv.php locale conversion}
* to allow decimal point to be locale specific.
*
* @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])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str); }

// my solution : return (bool) true ; :)

** but what is that error for??? **

@masthisis
Copy link
Author

@neo22s @Azuka @stik @juherr

@WinterSilence
Copy link
Contributor

WinterSilence commented Apr 4, 2019

@mas1375 php 7.3? add dump localeconv()

rework:

/**
 * Checks whether a value is a valid number (negative and decimal numbers allowed).
 *
 * @param   mixed  $value
 * @return  boolean
 */
public static function numeric($value)
{
    if (is_numeric($value))
    {
        if ((intval($value) === $value) OR (floatval($value) === $value))
        {
            return TRUE;
        }
        // To make sure the value contains at least one digit (before/after the decimal point)
        $locale = localeconv();
        return (bool) preg_match(
            '/^-?+(?=.*\d)\d*+['.preg_quote($locale['decimal_point']).']?+\d*+$/D',
            $value
        );
    }
    return FALSE;
}

@masthisis
Copy link
Author

@WinterSilence PHP 7.2.
thanks.

@WinterSilence
Copy link
Contributor

WinterSilence commented Apr 4, 2019

@mas1375 show dump localeconv(), changes in regexp only in php 7.3 - i think problem in incorrect pointer.

@WinterSilence WinterSilence added this to the 4.0.0 milestone Apr 4, 2019
@toitzi
Copy link
Member

toitzi commented Apr 28, 2019

@mas1375 @WinterSilence cannot reproduce this one. Is it a error / issue of the framework?

@masthisis
Copy link
Author

masthisis commented Apr 28, 2019

it's localeconv() dump :

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

and the $str value is : '26' (string) $str)

@WinterSilence
Copy link
Contributor

WinterSilence commented Apr 29, 2019

@toitzi as i already say, problem in 'decimal_point' => string '/' (length=1). This class use many strange and legacy solutions :(

@WinterSilence WinterSilence added the bug Verified Bug label Apr 29, 2019
@toitzi
Copy link
Member

toitzi commented Apr 29, 2019

@WinterSilence Thanks for clarification!

@toitzi
Copy link
Member

toitzi commented Apr 29, 2019

@WinterSilence Does your code above fix it? Can you make a PR out of it?

@WinterSilence
Copy link
Contributor

WinterSilence commented Apr 29, 2019

@toitzi i cant: new github plugin for atom ide use direct pull (i think its bug), i cant use old plugin - ide not support it

@masthisis
Copy link
Author

masthisis commented Apr 29, 2019

@mas1375 php 7.3? add dump localeconv()

rework:

/**
 * Checks whether a value is a valid number (negative and decimal numbers allowed).
 *
 * @param   mixed  $value
 * @return  boolean
 */
public static function numeric($value)
{
    if (is_numeric($value))
    {
        if ((intval($value) === $value) OR (floatval($value) === $value))
        {
            return TRUE;
        }
        // To make sure the value contains at least one digit (before/after the decimal point)
        $locale = localeconv();
        return (bool) preg_match(
            '/^-?+(?=.*\d)\d*+['.preg_quote($locale['decimal_point']).']?+\d*+$/D',
            $value
        );
    }
    return FALSE;
}

there is another solution for it, according to the manual :

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.

so :

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

masthisis added a commit to masthisis/koseven that referenced this issue Apr 30, 2019
 - fixed an issue with preg_quote() .
 - Closes koseven#351
@masthisis masthisis mentioned this issue Apr 30, 2019
9 tasks
@toitzi toitzi closed this as completed May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified Bug
Development

No branches or pull requests

3 participants