How to use PHP string truncation functions-PHP Tutorial-php Tutorial

Source: Internet
Author: User
Experts teach you how to use PHP string truncation functions. When learning PHP, you may encounter the PHP string truncation problem. here we will introduce the solution to the PHP string truncation function problem. here we will share it with you. PHP itself is a way of learning PHP. you may encounter the PHP string truncation problem. here we will introduce the solution to the PHP string truncation function problem. I will share it with you here. PHP itself is a simple and powerful language. PHP has core features such as powerful string and array processing capabilities, and greatly improved support for object-oriented programming (PHP5 and later versions ).

Using standard and optional extension modules, PHP applications can connect to more than a dozen databases such as MySQL or Oracle, draw, create PDF files, and create parsed XML files. You can also use the C language to write your own PHP extension module. For example, provide a PHP interface function in an existing code library. You can also run PHP in Windows, use COM to control other Windows applications such as Word and Excel, or use ODBC to connect to the database. In China, PHP is a commonly used network programming language.

ASP3.0 has already been abandoned by Microsoft and will not be updated any more. some people may think that PHP should also be something that will soon become a new thing, right? However, on the contrary, PHP is not only developing rapidly, but also leaving ASP far behind. it is gradually becoming the most widely used and popular language on the Internet. Next, let's use the powerful PHP language to solve this problem.

PHP string truncation function code:

 
 
  1. php
  2. functionget_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. returnsubstr($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. returnsubstr($string,$start,$length);
  53. }
  54. elseif($length==0)
  55. {
  56. return;
  57. }
  58. else
  59. {
  60. $length=strlen($string)-abs($length)-$start;
  61. returnget_substr($string,$start,$length);
  62. }
  63. }
  64. else
  65. {
  66. $start=strlen($string)-abs($start);
  67. returnget_substr($string,$start,$length);
  68. }
  69. }
  70. ?>

Bytes. PHP itself is a kind...

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.