[英]How can I use a mask in values came from database?
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.
我的代码很简单,有一个表格从数据库中获取数据,但我想用面具显示我的手机:(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
.您可以使用简单的正则表达式替换将所有数字替换为
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.最简单的方法是删除 php echo phone 并替换为硬编码文本。 Like that
<td>xxx-xxx-xxx</td>
.就像那个
<td>xxx-xxx-xxx</td>
。
You tagged your question with javascript.您用 javascript 标记了您的问题。 Javascript version: Only important would be the regex pattern.
Javascript 版本:唯一重要的是正则表达式模式。
/(\d{2})(\d{5})(\d+)/
javascript example javascript 示例
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>
.将此行与电话号码从
<td>{{$delivery_boys->delivery_boy_phone}}</td>
更改为<td class="phonenumbers">{{$delivery_boys->delivery_boy_phone}}</td>
。 Now, you have a seelctor and can fetch the lines and modified by js.现在,你有了一个 seelctor,可以获取行并由 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.