2

Different format number depending on mobile phone or business

 2 years ago
source link: https://www.codesd.com/item/different-format-number-depending-on-mobile-phone-or-business.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Different format number depending on mobile phone or business

advertisements

Currently in my php application I have the following code that formats the unformatted 10 digit australian phone number to XX XXXX XXXX. What I would like to do is if the phone number in the database starts with 04 then it will format it as 4,3,3 like XXXX XXX XXX otherwise will maintain the 2,4,4 formatting. All phone numbers are stored in the database as XXXXXXXXXX.

Here is my code so hopefully someone can shed some light. I know there needs to be an if statement, but unsure how to check the first 2 digits of the number for the 04.

<?php

    $num = $record['busphone'];
    $phoneformated = substr($num,0,2)." ".substr($num,4,4)." ".substr($num,6);

    echo "Ph: " . $phoneformated;

?>


Compare the substring in an if statement:

if (substr($num, 0, 2) == '04') {
    $phoneformatted = substr($num, 0, 4) . " " . substr($num, 4, 3) . " " . substr($num, 7);
} else {
    $phoneformatted = substr($num, 0, 2) . " " . substr($num, 2, 4) . " " . substr($num, 6);
}

Notice that you had a typo in formatting the residential numbers, substr($num, 4, 4) should be substr($num, 2, 4).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK