PHP Replacement String Intermediate character is an ellipsis method, string ellipsis _php tutorial

Source: Internet
Author: User
Tags dot net

PHP replaces the middle character of the string with the ellipsis method, the string ellipsis


The example in this article describes the method of omitting the intermediate characters of the PHP replacement string. Share to everyone for your reference. The specific analysis is as follows:

For a long string, if you just want the user to see part of the tail and the middle, you can use this PHP function, he can specify the number of intermediate strings to hide

/** * 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 in XHTML do T 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] = = ") {$tmp _string = substr ($tmp _strin G, 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] == ') {$tmp _string = substr ($tmp _string, 0, $i);    $return. = Strrev ($tmp _string);  } $i + +;  } return $return; Return substr ($string, 0, $start). $rep. substr ($string,-$end);}

Demo Example:

Example: $text = ' This was a very long test sentence, bla foo bar nothing ';p rint strmiddlereducewordsensitive ($text, 30) . "\ n";//Returns:this is a very[...] Foo Bar Nothing (~ chrs) Print strmiddlereducewordsensitive ($text, 30, ' ... '). "\ n";//Returns:this is a very...foo bar nothing (~ chrs)

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/994386.html www.bkjia.com true http://www.bkjia.com/PHPjc/994386.html techarticle PHP replaces the middle character of the string with the ellipsis method, the string ellipsis This example describes the PHP replacement string intermediate character is an ellipsis method. Share to everyone for your reference. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.