繁体   English   中英

如何在来自数据库的值中使用掩码?

[英]How can I use a mask in values came from database?

我正在从我的数据库中获取一些数据,并希望在我的视图中对其进行格式化。 我的代码很简单,有一个表格从数据库中获取数据,但我想用面具显示我的手机:(12) 94832-3823。 尝试了各种操作系统的东西,但没有成功,谁能给我一个提示?

我的表代码:

          <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!

您可以使用简单的正则表达式替换将所有数字替换为X

因此,在您要显示“蒙面”电话号码的地方,您可以放置:

preg_replace('/\d/', 'X', '+5511920140349');

最简单的方法是删除 php echo 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) )

附录

将此行与电话号码从<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM