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:
- <?php
-
- functionget_substr($string,$start='0',$length='')
- {
- $start=(int)$start;
- $length=(int)$length;
- $i=0;
- if(!$string)
- {
- return;
- }
- if($start>=0)
- {
- while($i<$start)
- {
- if(ord($string[$i])>127)
- {
- $i=$i+2;
- }
- else
- {
- $i++;
- }
- }
- $start=$i;
- if($length=='')
- {
- returnsubstr($string,$start);
- }
- elseif($length>0)
- {
- $end=$start+$length;
- while($i<$end)
- {
- if(ord($string[$i])>127)
- {
- $i=$i+2;
- }
- else
- {
- $i++;
- }
- }
- if($end!=$i-1)
- {
- $end=$i;
- }
- else
- {
- $end--;
- }
- $length=$end-$start;
- returnsubstr($string,$start,$length);
- }
- elseif($length==0)
- {
- return;
- }
- else
- {
- $length=strlen($string)-abs($length)-$start;
- returnget_substr($string,$start,$length);
- }
- }
- else
- {
- $start=strlen($string)-abs($start);
- returnget_substr($string,$start,$length);
- }
- }
-
- ?>