H
H
hollanditkzn2017-08-14 13:55:39
Yii
hollanditkzn, 2017-08-14 13:55:39

Maybe there are other simple solutions in yii2?

I have a number, I didn’t get it before, now I had to convert the phone number into a telephone format, before it was a regular phone number without restrictions. Let's say 89274434521 was stored like this, now I need to convert it like this 8 (927) 443-45-21. I found this solution on the Internet, but it seemed too much code, maybe there is some kind of ready-made solution for yii2?

<div><?php $s = $model->phone;
        echo $s[0].' ('.$s[1].$s[2].$s[3].') '.$s[4].$s[5].$s[6].'-'.$s[7].$s[8].'-'.$s[9].$s[10]; ?></div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2017-08-14
@hollanditkzn

$phone = '89274434521';

$formatted = preg_replace(
    '/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/i',
    '$1 ($2) $3-$4-$5',
    $phone
);

echo $formatted; # 8 (927) 443-45-21

V
vyachin, 2017-08-14
@vyachin

https://github.com/giggsey/libphonenumber-for-php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question