简体   繁体   中英

Convert a number to international, PHP

In PHP what is the quickest way to convert an mobile number to international format:

So 07123456789 becomes 447123456789.

I have tried a few ways and cant seem to get it to work.

This is current script:

    if(strlen($gsm) > 2) {
        if(!substr_compare($gsm, "07", 0, 2, false)) {
            unset($gsm);
        }
        elseif (substr_compare($gsm, "07", 0, 3, true)) {
            if(strlen($gsm) == 11) {
                return "447" . substr($gsm, 2);
            }
        }
    }

Note: This script only runs if the number matches a regex.

Something like this, perhaps?

$intl_number = preg_replace('/^0/','44',$uk_number);

or if you specifically only want to do UK mobile numbers:

$intl_number = preg_replace('/^07/','447',$uk_mob_number);

(note: I'm assuming UK-specific since you specified '44' in the question)

This does use Regex, but should be pretty quick in execution speed since it is anchored to the begining of the string.

Fastest way that comes to mind for me, as long as you are shre the number matches the format 07xxxxxx at this point:

 $number = "07123456789";
 $number = '44'.substr($number,1);

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