jairogill Posted October 14, 2017 Share Posted October 14, 2017 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! Quote Link to comment https://forums.phpfreaks.com/topic/305357-add-dash-in-different-length-phone-number-in-php/ Share on other sites More sharing options...
archive Posted October 14, 2017 Share Posted October 14, 2017 You haven't assigned $data with the phone number that you want to format. Quote Link to comment https://forums.phpfreaks.com/topic/305357-add-dash-in-different-length-phone-number-in-php/#findComment-1552705 Share on other sites More sharing options...
Barand Posted October 14, 2017 Share Posted October 14, 2017 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>'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/305357-add-dash-in-different-length-phone-number-in-php/#findComment-1552706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.