隐藏部分电话号码,直到点击。

huangapple go评论57阅读模式
英文:

Hide partial phone number until clicked

问题

I am wanting to hide part of a phone number until clicked on using php. I came across this solution to mask the middle numbers which works well. However, I would like to mask the last 6 numbers and then have the number clickable to reveal the full number. Some numbers will be longer than others, so not all X characters long.

This is the solution code from another question. I have $number from user input that is inserted using some code in my php file, e.g. epa_member( 'mobile_number' ) which displays the number.

<?php

  $number = epa_member('mobile_number'); /* inserts member, phone number, e.g. 0414555000 */
  $middle_string = "";
  $length = strlen($number);

  if ($length < 3) {
    echo $length == 1 ? "*" : "*" . substr($number, -1);
  } else {
    $part_size = floor($length / 3);
    $middle_part_size = $length - ($part_size * 2);
    for ($i = 0; $i < $middle_part_size; $i++) {
       $middle_string .= "*";
    }
    echo  substr($number, 0, $part_size ) . $middle_string  . substr($number,  - $part_size );
 }
 ?>
英文:

I am wanting to hide part of a phone number until clicked on using php. I came across this solution to mask the middle numbers which works well. However, I would like to mask the last 6 numbers and then have the number clickable to reveal the full number. Some numbers will be longer than others, so not all X characters long.

This is the solution code from another question. I have $number from user input that is inserted using some code in my php file, e.g. epa_member( 'mobile_number' ) which displays the number.

&lt;?php

  $number = epa_member ( &#39;mobile_number&#39; ); /* inserts member, phone number, e.g. 0414555000 */
  $middle_string =&quot;&quot;;
  $length = strlen($number);

  if( $length &lt; 3 ){
    echo $length == 1 ? &quot;*&quot; : &quot;*&quot;. substr($number,  - 1);
  }
  else
  {
    $part_size = floor( $length / 3 ) ;
    $middle_part_size = $length - ( $part_size * 2 );
    for( $i=0; $i &lt; $middle_part_size ; $i ++ ){
       $middle_string .= &quot;*&quot;;
    }
    echo  substr($number, 0, $part_size ) . $middle_string  . substr($number,  - $part_size );
 }
 ?&gt;

Thanks

答案1

得分: 2

这是代码的翻译部分:

对于 PHP

$number = '0414555000';
$length = strlen($number);
$mask_length = 6;
if ($mask_length < 6) {
    // 对于少于6位的情况,您可以按照您的需求进行更新
} else {
    $unmask_length = $length - 6;
    $unmask_digits = substr($number, 0, $unmask_length);
    $mask_chars = str_split(substr($number, -$mask_length));
    $mask_digits = '';
    foreach ($mask_chars as $char) {
        $mask_digits .= "<span class='mask-character' data-char='" . $char . "'>*</span>";
    }
}
echo "<div id='phone_number'>" . $unmask_digits . $mask_digits . "</div>";

在这里,我保留了对于少于6位数字的条件。您可以根据需要进行调整。在这之后,最后的6位数字将被掩盖,并以id为"phone_number"的div中的形式呈现。

现在对于 JavaScript,我假设当用户点击id为"phone_number"的div时,掩盖的字符将对用户可见:

document.querySelector("#phone_number").addEventListener("click", (e) => {
    document.querySelectorAll(".mask-character").forEach((char) => {
        char.innerText = char.dataset.char;
    });
});

用于快速测试的HTML代码如下,这是在PHP解析代码后渲染的,还链接了JavaScript:

<div id="phone_number">0414
  <span class="mask-character" data-char="5">*</span>
  <span class="mask-character" data-char="5">*</span>
  <span class="mask-character" data-char="5">*</span>
  <span class="mask-character" data-char="0">*</span>
  <span class="mask-character" data-char="0">*</span>
  <span class="mask-character" data-char="0">*</span>
</div>

希望这对您有所帮助。

英文:

You need javascript also with php for this. Here is a code, try this:-

For PHP:-

$number = &#39;0414555000&#39;;
$length = strlen($number);
$mask_length = 6;
if($mask_length &lt; 6){
  // For Less than 6 digits, you can update as you wish
}else{
  $unmask_length = $length - 6;
  $unmask_digits = substr($number,0,$unmask_length);
  $mask_chars = str_split(substr($number,-$mask_length));
  $mask_digits = &#39;&#39;;
  foreach($mask_chars as $char){
	$mask_digits .= &quot;&lt;span class=&#39;mask-character&#39; data-char=&quot; . $char . &quot;&gt;*&lt;/span&gt;&quot;;
  }
}
echo &quot;&lt;div id=&#39;phone_number&#39;&gt; &quot; . $unmask_digits . $mask_digits . &quot;&lt;/div&gt;&quot;;

I leave the if condition, for digits less than 6. You can adjust as you you wish. After this, the last 6 digits will be masked. I render the digits inside a div with id "phone_number".

Now on JavaScript, I am assuming when user click on div with id "phone_number", the masked characters will be visible to user:-

document.querySelector(&quot;#phone_number&quot;).addEventListener(&quot;click&quot;, (e) =&gt; {
   document.querySelectorAll(&quot;.mask-character&quot;).forEach((char) =&gt; {
     char.innerText = char.dataset.char;
   });
});

For Quick Testing here, I am pasting the html that is rendered after PHP parse the code, and also linked the JavaScript:-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&quot;#phone_number&quot;).addEventListener(&quot;click&quot;, (e) =&gt; {
   document.querySelectorAll(&quot;.mask-character&quot;).forEach((char) =&gt; {
        char.innerText = char.dataset.char;
   });
})

<!-- language: lang-html -->

&lt;div id=&quot;phone_number&quot;&gt; 0414
  &lt;span class=&quot;mask-character&quot; data-char=&quot;5&quot;&gt;*&lt;/span&gt;
  &lt;span class=&quot;mask-character&quot; data-char=&quot;5&quot;&gt;*&lt;/span&gt;
  &lt;span class=&quot;mask-character&quot; data-char=&quot;5&quot;&gt;*&lt;/span&gt;
  &lt;span class=&quot;mask-character&quot; data-char=&quot;0&quot;&gt;*&lt;/span&gt;
  &lt;span class=&quot;mask-character&quot; data-char=&quot;0&quot;&gt;*&lt;/span&gt;
  &lt;span class=&quot;mask-character&quot; data-char=&quot;0&quot;&gt;*&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

Hope it will be helpful.

huangapple
  • 本文由 发表于 2023年5月14日 12:48:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定