Eamorr
Eamorr

Reputation: 10022

PHP and libphonenumber: convert a string to PhoneNumber object?

So I'm passing a phone number string to my PHP back-end. I'm trying to use Google's libphonenumber to perform a validation of the passed phone number.

Here is the string: "+447400123456" ($val)

$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$phoneNumber = new \libphonenumber\PhoneNumber().setNationalNumber($val);
error_log(json_encode($phoneNumber));
$internationalFormatPhoneNumber = $phoneUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
error_log(json_encode($internationalFormatPhoneNumber));

Unfortunately, I keep getting

"local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined function App\Http\Controllers\setNationalNumber()'"

All I want to do is check if the passed string is a valid international phone number (including mobiles) before I store it in my database.

Can anyone help me with this? I'm struggling with the documentation and keep getting errors.

Upvotes: 1

Views: 2285

Answers (1)

honerlawd
honerlawd

Reputation: 1509

You are using a dot operator . instead of the correct object operator -> so you just need to replace the .setNationalNumber($val) to ->setNationalNumber($val).

Upvotes: 3

Related Questions