Home > Article > Backend Development > How to intercept string length in php
PHP String Get
is used to get the specified string from the string.
Relevant functions are as follows:
substr(): Get a part of it from a string.
strstr(): Finds the first occurrence of a string in another string and returns all characters from that position to the end of the string.
subchr(): Same as strstr()
strrchr(): Find the last occurrence of a string in another string and return all characters from that position to the end of the string character.
Related recommendations: "PHP Tutorial"
substr()
substr() function is used to extract from a string Get part of it and return a string.
Syntax:
string substr ( string string, int start [, int length] )
Parameter description is as follows:
Example:
<?php echo substr('abcdef', 1); //输出 bcdef echo substr('abcdef', 1, 2); //输出 bc echo substr('abcdef', -3, 2); //输出 de echo substr('abcdef', 1, -2); //输出 bcd ?>
Tips
If start is negative and length is less than or equal to start, length is 0.
strstr()
Find the first occurrence of a string in another string and return all characters from that position to the end of the string, Returns FALSE if not found.
Syntax:
string strstr ( string string, string needle )
Parameter description is as follows:
Example:
<?php $email = 'user@5idev.com'; $domain = strstr($email, '@'); echo $domain; // 输出 @5idev.com ?>
Tips
This function is case sensitive. For case-insensitive search, use stristr() .
strchr()
Same as strstr().
strrchr()
Find the last occurrence of a string in another string and return all characters from that position to the end of the string, if If not found, return FALSE.
Syntax:
string strrchr ( string string, string needle )
This function behaves the same as the strstr() function. For the meaning of the parameters, please refer to the strstr() function parameter description above.
Example:
<?php $str="AAA|BBB|CCC"; echo strrchr($str, "|"); ?>
Run the example, output:
|CCC
Combined with the substr() function, you can intercept all the content after the last character that appears:
<?php $str="AAA|BBB|CCC"; echo substr(strrchr($str, "|"), 1); ?>
The above is the detailed content of How to intercept string length in php. For more information, please follow other related articles on the PHP Chinese website!