PHP Digest Generation function (custom, no garbled)

Source: Internet
Author: User
  1. /**
  2. PHP Digest Generation function
  3. Link:http://bbs.it-home.org 2013-3-7
  4. */
  5. function Cutstr ($string, $length, $charset, $dot) {//characters, intercept length, character set, end symbol
  6. if (strlen ($string) <= $length) {
  7. return $string;
  8. }
  9. $pre = Chr (1);
  10. $end = Chr (1);
  11. Protect Special strings
  12. $string = str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array ($pre. ') & '. $end, $pre. ' ". $end, $pre. ' < '. $end, $pre. ' > '. $end), $string);
  13. $strcut = ";
  14. if (Strtolower ($charset) = = ' Utf-8 ') {
  15. $n = $tn = $noc = 0;
  16. while ($n < strlen ($string)) {
  17. $t = Ord ($string [$n]);
  18. if ($t = = 9 | | $t = = 10 | | (<= $t && $t <= 126)) {
  19. $tn = 1; $n + +; $noc + +;
  20. } elseif (194 <= $t && $t <= 223) {
  21. $tn = 2; $n + = 2; $noc + = 2;
  22. } elseif (224 <= $t && $t <= 239) {
  23. $tn = 3; $n + = 3; $noc + = 2;
  24. } elseif (<= $t && $t <= 247) {
  25. $tn = 4; $n + = 4; $noc + = 2;
  26. } elseif (248 <= $t && $t <= 251) {
  27. $tn = 5; $n + = 5; $noc + = 2;
  28. } elseif ($t = = 252 | | $t = = 253) {
  29. $tn = 6; $n + = 6; $noc + = 2;
  30. } else {
  31. $n + +;
  32. }
  33. if ($noc >= $length) {
  34. Break
  35. }
  36. }
  37. if ($noc > $length) {
  38. $n-= $tn;
  39. }
  40. $strcut = substr ($string, 0, $n);
  41. } else {
  42. for ($i = 0; $i < $length; $i + +) {
  43. $strcut. = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i];
  44. }
  45. }
  46. To restore a special string
  47. $strcut = Str_replace (Array ($pre. ' & '. $end, $pre. ' ". $end, $pre. ' < '. $end, $pre. ' > '. $end), Array (' & ', ' "', ' < ', ' > '), $strcut);
  48. Fixed an issue where special string segments occurred
  49. $pos = Strrpos ($s, Chr (1));
  50. if ($pos!== false) {
  51. $strcut = substr ($s, 0, $pos);
  52. }
  53. return $strcut. $dot;
  54. }
  55. ?>
Copy Code

The above is the full code of the PHP digest generation function, you copy to your own editor, and then test run it.

  • Related Article

    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.