PHP intercept string function, PHP custom string interception method

Source: Internet
Author: User
  1. /**
  2. * Method Library-intercept string
  3. * @param string $string strings
  4. * @param int$length character length
  5. * @param string $dot whether to add after interception ...
  6. * @param string $charset encoding
  7. * @return String
  8. */
  9. Public Function Cutstr ($string, $length, $dot = ' ... ', $charset = ' utf-8 ') {
  10. if (strlen ($string) <= $length) {
  11. return $string;
  12. }
  13. $string = Str_replace (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string);
  14. $strcut = ";
  15. if (Strtolower ($charset) = = ' Utf-8 ') {
  16. $n = $tn = $noc = 0;
  17. while ($n < strlen ($string)) {
  18. $t = Ord ($string [$n]); Ascii?
  19. if ($t = = 9 | | $t = = 10 | | (<= $t && $t <= 126)) {
  20. $tn = 1; $n + +; $noc + +;
  21. } elseif (194 <= $t && $t <= 223) {
  22. $tn = 2; $n + = 2; $noc + = 2;
  23. } elseif (224 <= $t && $t < 239) {
  24. $tn = 3; $n + = 3; $noc + = 2;
  25. } elseif (<= $t && $t <= 247) {
  26. $tn = 4; $n + = 4; $noc + = 2;
  27. } elseif (248 <= $t && $t <= 251) {
  28. $tn = 5; $n + = 5; $noc + = 2;
  29. } elseif ($t = = 252 | | $t = = 253) {
  30. $tn = 6; $n + = 6; $noc + = 2;
  31. } else {
  32. $n + +;
  33. }
  34. if ($noc >= $length) {
  35. Break
  36. }
  37. }
  38. if ($noc > $length) {
  39. $n-= $tn;
  40. }
  41. $strcut = substr ($string, 0, $n);
  42. } else {
  43. for ($i = 0; $i < $length; $i + +) {
  44. $strcut. = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i];
  45. }
  46. }
  47. $strcut = Str_replace (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $strcut);
  48. return $strcut. $dot;
  49. }
Copy Code

Example 2,php detects if a string exists:

    1. /**
    2. * Method Library-whether a string exists
    3. * @param string $str: Character or string
    4. * @param string $string: String
    5. * @return String Example: $str = ' $string ' = ' 1234 ' returns TRUE
    6. */
    7. Public Function Is_str_exist ($STR, $string) {
    8. $string = (string) $string;
    9. $str = (string) $str;
    10. Return Strstr ($string, $str) ===false? False:true;
    11. }
Copy Code
  • 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.