preg-matchereg

Updating from ereg to preg_match


I read similar titles but I couldn't make it run..

Now, I have a code like this (originally ereg):

        if (preg_match("[^0-9]",$qrcode_data_string)){
        if (preg_match("[^0-9A-Z \$\*\%\+\-\.\/\:]",$qrcode_data_string)) {

I also tried using / at the beginning and end of rule but didn't work.

Any replies welcome.


Solution

  • With the preg_* functions you need delimiters around the pattern:

    if (preg_match("#[^0-9]#", $qrcode_data_string)) {
    #               ^      ^
    

    From the documentation:

    When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

    Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).