I'm getting some data from my database and wanted to format it inside my view. My code is simple, with a table bringing the data from database, but i want to show my phone with a mask: (12) 94832-3823. Tried all sort os things, but no success, can anyone give me a hint?
My table code:
<thead>
<tr>
<th>{{ __('messages.serial no')}}</th>
<th>{{ __('messages.Delivery_Boy')}}</th>
<th>{{ __('messages.Delivery_Boy_Image')}}</th>
<th>{{ __('messages.Delivery_Boy_Phone')}}</th>
<th>{{ __('messages.Status')}}</th>
<th>{{ __('messages.action')}}</th>
</tr>
</tfoot>
<tbody>
@if(count($delivery_boy)>0)
@php $i=1; @endphp
@foreach($delivery_boy as $delivery_boys)
<tr>
<td>{{$i}}</td>
<td>{{$delivery_boys->delivery_boy_name}}</td>
<td align="center"><img src="{{url($delivery_boys->delivery_boy_image)}}" style="width: 21px;"></td>
<td>{{$delivery_boys->delivery_boy_phone}}</td>
<td>
@if($delivery_boys->is_confirmed==0)
<a href="{{route('confirm.delivery.status',[$delivery_boys->delivery_boy_id,'1'])}}" class="btn btn-info" style="color: #fff;">Yes</a>
<a href="{{route('confirm.delivery.status',[$delivery_boys->delivery_boy_id,'2'])}}" class="btn btn-danger" style="color: #fff;">No</a>
@elseif($delivery_boys->is_confirmed == 1)
<span style="color:green;">Aprovado</span>
@else
<span style="color:red;">Reprovado</span>
@endif
</td>
<td>
<a href="{{route('edit-delivery_boy',$delivery_boys->delivery_boy_id)}}" style="width: 28px; padding-left: 6px;" class="btn btn-info" style="width: 10px;padding-left: 9px;" style="color: #fff;"><i class="fa fa-edit" style="width: 10px;"></i></a>
<button type="button" style="width: 28px; padding-left: 6px;" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal{{$delivery_boys->delivery_boy_id}}"><i class="fa fa-trash"></i></button>
</td>
</tr>
@php $i++; @endphp
@endforeach
@else
<tr>
<td>No data found</td>
</tr>
@endif
</tbody>
</table>```
How's it displaying now:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/2LOpY.png
Thanks for your time!
What you could do with a simple regex replace, is replacing all the digits into a X
.
So on the place where you want to show the 'masked' phone number you can place this:
preg_replace('/\d/', 'X', '+5511920140349');
The easiest way would be to remove the php echo phone and replace with hard coded text. Like that <td>xxx-xxx-xxx</td>
.
You tagged your question with javascript. Javascript version: Only important would be the regex pattern. /(\d{2})(\d{5})(\d+)/
javascript example
function formatPhoneNumber(phoneNumberString) { var cleaned = ('' + phoneNumberString).replace(/\D/g, ''); var match = cleaned.match(/(\d{2})(\d{5})(\d+)/); if (match) { return '(' + match[1] + ') ' + match[2] + '-' + match[3]; } return null; } p = "+5511920140349" console.log( formatPhoneNumber(p) )
Change this line with the phone number from <td>{{$delivery_boys->delivery_boy_phone}}</td>
to <td class="phonenumbers">{{$delivery_boys->delivery_boy_phone}}</td>
. Now, you have a seelctor and can fetch the lines and modified by js. like that:
const nums = document.querySelectorAll('.phonenumber'); console.log(nums) nums.forEach(td => { const p = td.innerHTML; td.innerHTML = formatPhoneNumber(p) }) function formatPhoneNumber(phoneNumberString) { var cleaned = ('' + phoneNumberString).replace(/\D/g, ''); var match = cleaned.match(/(\d{2})(\d{5})(\d+)/); if (match) { return '+(' + match[1] + ') ' + match[2] + '-' + match[3]; } return null; }
<table border="1px"> <tr> <td>abc</td> <td class="phonenumber">123456789</td> </tr> <tr> <td>abc</td> <td class="phonenumber">123456789</td> </tr> <tr> <td>abc</td> <td class="phonenumber">123456789</td> </tr> </table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.