How to Use PHP string truncation Functions

Source: Internet
Author: User

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

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.