简体   繁体   中英

PHP Regular expressions not working

I've been using regexr.com to build and test regular expressions to use in my php programs. However, with this one particular expression, it seems to work on the website but not in the program and I am not sure why

I am trying to match EU label data (ex: GC 71) in the following string:

"Viking SnowTech II 165/70 R13 79T Winterreifen Auf Lager  G C 71  dBKundenbewertung (2.00) 29,50 € Details  In den Einkaufswagen"

the regular expression '#\\s\\w\\s\\w\\s\\d{1,}#' works fine on regexr.com but finds no match in my program

$eulab = $row_html -> plaintext;
        if( preg_match('#\s\w\s\w\s\d{1,}#', $eulab)){
            $insert['eulab'] =  $matches[0];

        }else {
            echo "No Match";
        }

i've tried other expressions too like \\s{1}[AZ]{1}\\s{1}[AZ]{1}\\s{1}\\d{1,}\\s{1} which also work on regexr but find no match in my program. Could this be an encoding issue? or just my php syntax? No errors are being thrown.

My problem was that the browser was only displaying a single whitespace when really there was more than that. I altered the expression to correct for that and it worked.

problem solved by @chris85 above

My final code and expression are:

        $eulab = $row_html -> plaintext;

        if( preg_match('#\s\w\s+\w\s+\d+#', $eulab, $matches)){

            $insert['eulab'] =  str_replace(" ", "", $matches[0]);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM