Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Substr (source string, position [, length])- Intercepts the string to return part of the string


      
$str ="phpddt.com";
echo substr($str.2);//pddt.com
echo substr($str.2.3);//pdd
echo substr($str, -2);//om negative numbers are taken from the end
? >
Copy the code

But it’s easy to get garbled when you cut Chinese strings, because a Chinese character is two bytes and an English letter is one byte. The solution is as follows.

2. Mb_substr (), use the same method as substr, except that extension=php_mbstring.dll is enabled in php.ini.


      
echo mb_substr("PHP knows a lot".1.3."UTF-8");/ / HP
? >
Copy the code

The code is as follows:

substr(string,start,length)

The start parameter

Positive – starts at the specified position in the string

Negative – starts at the specified position at the end of the string

0 – starts at the first character in the string

The STRSTR () function searches for the first occurrence of one string in another.

This function returns the rest of the string (from the match point). Return false if the searched string is not found.

strstr('[email protected]', '@', TRUE); // set this parameter to true and return ABC before @

strstr( '[email protected]', '@'); // By default, the search value @ ends with @jb51.net

There are also many Chinese string interception tutorials on the Internet, the implementation is more complex, feel or use PHP built-in functions to achieve better. The compiled network data (PHP code) is as follows:

(1) Intercept GB2312 Chinese character string


      
// Intercepts the GB2312 Character string
function mysubstr($str.$start.$len){
$tmpstr ="";
$strlen = $start + $len;
for($i =0; $i < $strlen; $i{+ +)if(ord(substr($str.$i.1)) >0xa0) {$tmpstr .= substr($str.$i.2);
$i+ +; }else
$tmpstr .= substr($str.$i.1);
}
return $tmpstr;
}
echo mysubstr("PHP knows a lot".1.5);/ / PHP
? >
Copy the code

(2) Intercept utF8 encoded multi-byte string


      
// Intercepts the utf8 string
function utf8Substr($str.$from.$len)
{
return preg_replace('# ^ (? :[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'} '.
'((? :[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s'.'$1'.$str);
}
echo utf8Substr("PHP knows a lot".1.5);/ / HP dots
? >
Copy the code

(3) Support utF-8, GB2312 support Chinese character interception function


      
// Both utF-8 and GB2312 support Chinese character interception functions, the default encoding is UTF-8
function cut_str($string.$sublen.$start =0.$code ='UTF-8')
{
if($code= ='UTF-8')
{
$pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\ xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa.$string.$t_string);if(count($t_string[0]) -$start > $sublen)return join(' ', array_slice($t_string[0].$start.$sublen))."...";
return join(' ', array_slice($t_string[0].$start.$sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr =' ';for($i=0; $i<$strlen; $i{+ +)if($i> =$start && $i< ($start+$sublen))
{
if(ord(substr($string.$i.1)) >129)
{
$tmpstr.= substr($string.$i.2);
}
else
{
$tmpstr.= substr($string.$i.1); }}if(ord(substr($string.$i.1)) >129) $i+ +; }if(strlen($tmpstr) <$strlen ) $tmpstr. ="...";
return $tmpstr; }}$str ="PHP Diandian provides original PHP tutorials";
echo cut_str($str.8.0);// PHP diandian provides...
? >


Copy the code