Custom PHP string intercept function

Source: Internet
Author: User
Tags ord
Custom PHP string intercept function
  1. <>
  2. function Get_substr ($string, $start = ' 0 ', $length = ')
  3. {
  4. $start = (int) $start;
  5. $length = (int) $length;
  6. $i = 0;
  7. if (! $string)
  8. {
  9. Return
  10. }
  11. if ($start >=0)
  12. {
  13. while ($i < $start)
  14. {
  15. if (Ord ($string [$i]) >127)
  16. {
  17. $i = $i +2;
  18. }
  19. Else
  20. {
  21. $i + +;
  22. }
  23. }
  24. $start = $i;
  25. if ($length = = ")
  26. {
  27. Return substr ($string, $start);
  28. }
  29. ElseIf ($length >0)
  30. {
  31. $end = $start + $length;
  32. while ($i < $end)
  33. {
  34. if (Ord ($string [$i]) >127)
  35. {
  36. $i = $i +2;
  37. }
  38. Else
  39. {
  40. $i + +;
  41. }
  42. }
  43. if ($end! = $i-1)
  44. {
  45. $end = $i;
  46. }
  47. Else
  48. {
  49. $end--;
  50. }
  51. $length = $end-$start;
  52. Return substr ($string, $start, $length);
  53. }
  54. ElseIf ($length ==0)
  55. {
  56. Return
  57. }
  58. Else
  59. {
  60. $length = strlen ($string)-abs ($length)-$start;
  61. Return Get_substr ($string, $start, $length);
  62. }
  63. }
  64. Else
  65. {
  66. $start = strlen ($string)-abs ($start);
  67. Return Get_substr ($string, $start, $length);
  68. }
  69. }
  70. ?>
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.