PHP method to replace the middle character of a string with an ellipsis

怪我咯
Release: 2023-03-13 07:02:02
Original
1277 people have browsed it

This article mainly introduces the method of replacing the middle character of string with an ellipse in PHP. It can realize the function of replacing the middle part of the string with an ellipse, and is suitable for sensitive information such as account numbers and mobile phone numbers. Hidden, friends who need it can refer to

The example in this article describes the method of replacing the middle character of a string with an ellipse in PHP. Share it with everyone for your reference. The specific analysis is as follows:

For a long string, if you only want the user to see part of the head and tail content and hide the middle content, you can use this php function, which can specify the desired content. The number of hidden intermediate strings

/**
 * Reduce a string by the middle, keeps whole words together
 *
 * @param string $string
 * @param int $max (default 50)
 * @param string $replacement (default [...])
 * @return string
 * @author david at ethinkn dot com
 * @author loic at xhtml dot ne
 * @author arne dot hartherz at gmx dot net
 */
function strMiddleReduceWordSensitive($string,$max=50,$rep='[...]'){
  $strlen = strlen($string);
  if ($strlen <= $max)
    return $string;
  $lengthtokeep = $max - strlen($rep);
  $start = 0;
  $end = 0;
  if (($lengthtokeep % 2) == 0) {
    $start = $lengthtokeep / 2;
    $end = $start;
  } else {
    $start = intval($lengthtokeep / 2);
    $end = $start + 1;
  }
  $i = $start;
  $tmp_string = $string;
  while ($i < $strlen) {
    if (isset($tmp_string[$i]) and $tmp_string[$i] == &#39; &#39;) {
      $tmp_string = substr($tmp_string, 0, $i) . $rep;
      $return = $tmp_string;
    }
    $i++;
  }
  $i = $end;
  $tmp_string = strrev ($string);
  while ($i < $strlen) {
    if (isset($tmp_string[$i]) and $tmp_string[$i] == &#39; &#39;) {
      $tmp_string = substr($tmp_string, 0, $i);
      $return .= strrev ($tmp_string);
    }
    $i++;
  }
  return $return;
  return substr($string, 0, $start).$rep.substr($string, - $end);
}
Copy after login

Demonstration example:

// example:
$text = &#39;This is a very long test sentence, bla foo bar nothing&#39;;
print strMiddleReduceWordSensitive ($text, 30) . "\n";
// Returns: This is a very[...]foo bar nothing (~ 30 chrs)
print strMiddleReduceWordSensitive ($text, 30, &#39;...&#39;) . "\n";
// Returns: This is a very...foo bar nothing (~ 30 chrs)
Copy after login

The above is the detailed content of PHP method to replace the middle character of a string with an ellipsis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!