davideme / libphonenumber-for-PHP

PHP version of Google's phone number handling library
125 stars 71 forks source link

Matcher doesn't work due to newlines in the regular expressions #11

Open michaelarnauts opened 11 years ago

michaelarnauts commented 11 years ago

I've noticed that when the regular expression spans multiple lines (as most of them), the formatting doens't work correctly because the regular expression breaks.

This can be seen by trying to format the number +3224019700 (BE).

This can be solved by changing the __construct function of the Matcher class in PhoneNumberUtil.php to:

public function __construct($pattern, $subject)
{
    // Filter pattern
    $pattern = str_replace("\n", "", $pattern);
    $pattern = str_replace(" ", "", $pattern);

    $this->pattern = $pattern;
    $this->subject = $subject;
}
Nephele commented 11 years ago

This is the same issue as "isValidNumber fails on Windows", and a fix is included in an existing pull request.

michaelarnauts commented 11 years ago

I'm not sure it's the same issue. I'm reporting this on a linux machine, so the line-encodings should be \n. Also, the pull request fixes just one regexp.

Nephele commented 11 years ago

My impression was that the two issues are similar enough -- both are problems caused by failing to remove line breaks from the NationalNumberPattern regexps -- that they should both be fixed by the same patch. So if the existing pull request doesn't fix your problem, does that existing patch need to be changed?

In regards to your fix, I'm worried about indiscriminately removing all spaces from all regexps. For example, I know that some of the regexps to identify extensions intentionally include spaces as characters that need to be matched; removing those spaces from the pattern would break those regexps. So I'm guessing it would be preferable to isolate which specific regexps are causing the problem, and apply corrections only where necessary.

Personally, I've been testing the module both in linux and mac10.7, and haven't encountered this issue, which in part is why I incorrectly assumed that it was only happening in Windows. Just to be sure I understand the problem, does the following code snippet trigger the problem? $phoneUtil = PhoneNumberUtil::getInstance(); $phoneObj = $phoneUtil->parseAndKeepRawInput('+3224019700', 'BE'); $isValid = $phoneUtil->isValidNumber($phoneObj); echo "isValid=$isValid" . PHP_EOL; echo $phoneUtil->format($phoneObj, PhoneNumberFormat::INTERNATIONAL) . PHP_EOL; And is it triggered by the parse function, the isValidNumber function, or the format function?