International Mobile Numbers

15 posts by 3 authors in: Forums > CMS Builder
Last Post: January 31, 2011   (RSS)

Re: [Toledoh] International Mobile Numbers

By Jason - January 28, 2011

Hi Tim,

There was a small error in the function, try replacing it with this:

// Mobile Number Format
function format_mobile_number($mobile){
//strip out spaces
$mobile = str_replace(" ","",$mobile);
$tmpPhone = str_split($mobile);

if(@$tmpPhone[1]==4){

$tmpPhone[0]=61;
$phone=implode($tmpPhone);
}
else{
//error
return false;
}
return $phone;
}


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Toledoh - January 28, 2011

Almost there.

I think the final
return $phone; is meant to be return $mobile;

But now, the error checking to make sure it starts with "04" causes an error because its now starting with "614"

Should I keep the $phone and add it to a separate field in the database? use $mobile for display )in the local format of 0411..." and keep the $phone for use with SMS function? or change the error check to check for "04" OR "614"?

Thanks for this Jason - I know its beyond the call of duty!
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] International Mobile Numbers

By Jason - January 31, 2011 - edited: January 31, 2011

Hi Tim,

Here's a new version of the format_mobile_number function.

// converts a "04" mobile number to a "614" mobile number
function format_mobile_number($mobile){

//strip out spaces
$tmpPhone = str_replace(" ","",$mobile);
$tmpPhone = str_split($tmpPhone);

if(@$tmpPhone[1]==4){ //number is in the local format. Convert it to international format

$tmpPhone[0]=61;

$phone=implode($tmpPhone);
}
elseif(@$tmpPhone[0]==6 && @$tmpPhone[1]==1 && @$tmpPhone[2]==4){ //number is in international format. Return it as is.

$phone = implode($tmpPhone);
}
else{ //error. The number is in an invalid format.

return false;
}

return $phone;
}


In this version, if you send it a number in the format "04" it will convert it to a "614" format. If you pass it a "614" format, it will just give it back to you without changing it. If it's given a different format, it sends back "false" to indicate an error.

In addition, you could save two different mobile numbers in the database. You can store 1 they way they entered it, and then store the formatted one in a different field.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Toledoh - January 31, 2011

Woo Hoo!

Thanks!
Cheers,

Tim (toledoh.com.au)