PHP string interception function
substr function
Description : Implement interception of string
Syntax:
string substr(string $string,int $start [, int $length ])
Description: If length is omitted, the string from start to the end of the character is returned; if start or length is a negative number, it is reciprocal.
$str = 'javascript'; echo substr($str,0,4); //结果是 java echo substr($str,4); echo substr($str,-2);//得到pt
The other one is more special, if the length is a negative number then
$str = 'javascript'; echo substr($str,-5,-2);//得cri 即倒数第五开始到倒数第二的前一位
strstr function
Description: Will search for the first occurrence of a string in another string, case-sensitive
Syntax:
string strstr(string $haystack, mixed $needle)
Description: The function returns the rest of the string
Example:
<?php $str1 = "abcdef"; $str2 = "cd"; $str3 = strstr($str1,$str2); echo $str3;
The output result is cdef
stristr function
Description: Search characters The position of the first occurrence of a string in another string, but not case-sensitive
Syntax:
string stristr(string $haystack, mixed needle)
Related recommendations: "PHP Tutorial"
The above is the detailed content of How to intercept php string. For more information, please follow other related articles on the PHP Chinese website!