簡體   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