Skip to content
This repository has been archived by the owner on Aug 21, 2018. It is now read-only.

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

Open
michaelarnauts opened this issue Dec 20, 2012 · 3 comments
Open

Comments

@michaelarnauts
Copy link

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
Copy link

Nephele commented Feb 26, 2013

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

@michaelarnauts
Copy link
Author

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
Copy link

Nephele commented Feb 26, 2013

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants