Skip to content

Instantly share code, notes, and snippets.

@the7th
Created June 5, 2018 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the7th/776430818ccffb1cdf0a0d959f7141cf to your computer and use it in GitHub Desktop.
Save the7th/776430818ccffb1cdf0a0d959f7141cf to your computer and use it in GitHub Desktop.
Sanitize phone number for Malaysia
<?php
$prepare_phone_num = $phone_num;
$phone = $prepare_phone_num;
$phone = str_replace(["-", "–", " ", "+"], '', $phone);
if ("0" == substr($phone, 0, 1))
{
$phone = "6" . $phone;
}
if ("60" != substr($phone, 0, 2))
{
$phone = "60" . $phone;
}
return $phone;
@tajulasri
Copy link

kalau macam ini ?

function phone_number_formatter($phone) {

    $phone = str_replace(["-", "", " ", "+"], '', $phone);
    if(substr($phone,0,1) == '0') {

        return "6".$phone;
    }

    return "60".$phone;
}


echo phone_number_formatter('+143334567');

@tajulasri
Copy link

function phone_number_formatter($phone) {

    $phone = str_replace(["-", "", " ", "+"], '', $phone);
    return substr($phone,0,1) == '0' ?  "6".$phone : "60".$phone;
}


echo phone_number_formatter('+143334567');

@iqmal852
Copy link

iqmal852 commented Jun 6, 2018

mastaa tajulll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment