如何在来自数据库的值中使用掩码?
How can I use a mask in values came from database?
我正在从我的数据库中获取一些数据,并希望在我的视图中对其进行格式化。
我的代码很简单,table 从数据库中获取数据,但我想用掩码显示我的 phone:(12) 94832-3823。
尝试了所有 os 种方法,但没有成功,任何人都可以给我提示吗?
我的table代码:
<table class="table table-bordered" id="example10" width="100%" cellspacing="0">
<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>
现在显示如何:
感谢您的宝贵时间!
您可以使用简单的正则表达式替换,将所有数字替换为 X
。
所以在你想要显示 'masked' phone 号码的地方你可以放置这个:
preg_replace('/\d/', 'X', '+5511920140349');
最简单的方法是删除 php 回显 phone 并替换为硬编码文本。像那样<td>xxx-xxx-xxx</td>
.
更新
您用 javascript 标记了您的问题。 Javascript版本:
唯一重要的是正则表达式模式。 /(\d{2})(\d{5})(\d+)/
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) )
附录
将带有 phone 数字的这一行从 <td>{{$delivery_boys->delivery_boy_phone}}</td>
更改为 <td class="phonenumbers">{{$delivery_boys->delivery_boy_phone}}</td>
。现在,您有了一个 seelctor,可以获取行并由 js 修改。像那样:
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>
我正在从我的数据库中获取一些数据,并希望在我的视图中对其进行格式化。 我的代码很简单,table 从数据库中获取数据,但我想用掩码显示我的 phone:(12) 94832-3823。 尝试了所有 os 种方法,但没有成功,任何人都可以给我提示吗?
我的table代码:
<table class="table table-bordered" id="example10" width="100%" cellspacing="0">
<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>
现在显示如何:
感谢您的宝贵时间!
您可以使用简单的正则表达式替换,将所有数字替换为 X
。
所以在你想要显示 'masked' phone 号码的地方你可以放置这个:
preg_replace('/\d/', 'X', '+5511920140349');
最简单的方法是删除 php 回显 phone 并替换为硬编码文本。像那样<td>xxx-xxx-xxx</td>
.
更新
您用 javascript 标记了您的问题。 Javascript版本:
唯一重要的是正则表达式模式。 /(\d{2})(\d{5})(\d+)/
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) )
附录
将带有 phone 数字的这一行从 <td>{{$delivery_boys->delivery_boy_phone}}</td>
更改为 <td class="phonenumbers">{{$delivery_boys->delivery_boy_phone}}</td>
。现在,您有了一个 seelctor,可以获取行并由 js 修改。像那样:
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>