kaYko
kaYko

Reputation: 257

preg_match expression including white space

So I'm trying to parse a sequence of numbers that is extracted from an external web page.

preg_match('#([0-9]{3}\s[0-9]{3}\s[0-9]{3}){1}#', $element, $match);

won't return any result although echoing $element shows the correct expression:

341 102 408 AAA Bccc

Of course when I try the above regexp with above string on online regexp testers I got the expected result... I'm thinking that there may be an invisible caracter in place of a white space but can't find it. I'm getting really crazy...

EDIT:

Strangely this does remove every whitespace of the input string EXCEPT for the expression I'm trying to extract:

$element = preg_replace("/\s/", "", $element);

Upvotes: 1

Views: 984

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336478

Try

preg_match('#([0-9]{3}\s+[0-9]{3}\s+[0-9]{3}){1}#u', $element, $match);

to make the regex engine Unicode-aware, and to allow more than one whitespace character between digits; perhaps you have some non-ASCII whitespace in there.

That said, you can reduce this to

preg_match('#(?:[0-9]{3}\s+){2}[0-9]{3}#u', $element, $match);

and you will find the match result in $match[0] if it succeeded.

Upvotes: 2

Related Questions