Jump to content

Add dash in different length phone number in php


jairogill
 Share

Recommended Posts

Hi,

 

I am triying to add dash in the phone numbers when added, the phone numbers are added complete without spaces. like: 1234567890, and i want the output to be:

 

123-456-7890

 

Depending on where the number is from it will change, for ex.:

  • Italy is: +391234567890 (+39-123-456-7890)

  • Spain is: +34123456789 (+34-123-45-67-89)

and so on.

 

this is the code im triying to add it to:

<div class="btn-group btn-group-sm" role="group" aria-label="Extra-small button group">
        <a class="client_copy btn btn-gold" 

           data-client-name="<?php echo $emp['client_name']; ?>" 
           data-Client-email="<?php echo $emp['id_clients']; ?>" 
           data-client-phone="<?php echo $emp['phone_no']; ?>"
           data-client-id="<?php echo $emp['ID']; ?>"
           data-client-group="<?php echo $emp['group_name']; ?>"
           data-client-city="<?php echo '$' . $emp['myprice']; ?>"
           data-client-paymentmethod="<?php echo $emp['payment_method_name']; ?>"
           data-client-paymentvia="<?php echo $emp['payment_via_name']; ?>"
           data-client-paymentdetail="<?php echo $emp['payment_detail']; ?>"
           data-client-paymentdate="<?php echo $emp['payment_date']; ?>"
           data-client-customprice="<?php echo $emp['custom_price']; ?>"

           href="#modalCopyClient"><i class="fa  fa-files-o"></i></a>

So, the phone part is:

data-client-phone="<?php echo $emp['phone_no']; ?>"

i tried a couple solutions but i coulndt make it work

 

one of them was to add:

echo '('.substr($data, 0, 3).') '.substr($data, 3, 3).'-'.substr($data,6);

that may be the solution but it didnt work for me, i ended having an error

i add it this way:

data-client-phone="<?php echo '('.substr($data, 0, 3).') '.substr($data, 3, 3).'-'.substr($data,6); $emp['phone_no']; ?>"

and had this error:

(see image attached)

 

i know the error might be obvious but i just dont know that much about coding. sorry.

 

thanks!

post-205803-0-40173600-1507988137_thumb.png

Link to comment
Share on other sites

I was bored, so here's one way

function formatted($phone)
{
    if (strlen($phone)==10) {
        $prefix = '';
        $parts = [ 0 => 3,
                   3 => 3,
                   6 => 4   ];
    } else {
        $prefix = '+';
        $parts = [ 0 => 2,
                   2 => 3,
                   5 => 3,
                   8 => 4  ];
    }
    $results = [];
    foreach ($parts as $k=>$v) {
        $results[] = substr($phone, $k, $v);
    }
    return $prefix . join('-', $results);
}

/****************
*   Try it out  *
****************/
$nums  = ['441619871234' , '1234567890'];

foreach ($nums as $n) {
    echo formatted($n) . '<br>';
}

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.