PHP string get substr with strstr function PHP string get
Used to get the specified string from a string.
The relevant functions are as follows:
- SUBSTR (): Get a portion of it from a string
- Strstr (): Finds the position of the first occurrence of a string in another string and returns all characters from that position to the end of the string
- SUBCHR (): With Strstr ()
- STRRCHR (): Finds the last occurrence of a string in another string and returns all characters from that position to the end of the string
SUBSTR ()
The substr () function is used to get a portion of it from a string and returns a string.
Grammar:
String substr (string string, int start [, int length])
The parameters are described as follows:
Parameters |
Description |
String |
The string to process |
Start |
String start position, starting at 0, negative starts at the specified position at the end of the string |
Length |
Optionally, the string returns the length, by default, until the end of the string, and the negative is returned from the end of the string |
Example:
<?phpecho substr (' abcdef ', 1); Output Bcdefecho substr (' abcdef ', 1, 2); Output Bcecho substr (' abcdef ', -3, 2); Output Deecho substr (' abcdef ', 1,-2); Output bcd?>
Tips
If start is negative and length is less than or equal to start, length is 0.
Strstr ()
Finds the position of the first occurrence of a string in another string and returns all characters from that position to the end of the string, or FALSE if not found.
Grammar:
String Strstr (String string, String needle)
The parameters are described as follows:
Parameters |
Description |
String |
The string to process |
Needle |
The string to look for, and if it is a number, search for characters that match the ASCII value of the number |
Example:
<?php$email = ' [email protected] '; $domain = Strstr ($email, ' @ '); echo $domain;//Output @5idev.com?>
Tips
The function is case-sensitive. For case-insensitive lookups, use STRISTR ().
STRCHR ()
With Strstr ().
STRRCHR ()
Finds the position of the last occurrence of a string in another string and returns all characters from that position to the end of the string, or FALSE if not found.
Grammar:
String STRRCHR (String string, String needle)
The function behaves the same as the Strstr () function, and the parameter meaning can be found in the above strstr () function parameter description.
Example:
<?php$str= "aaa| bbb| CCC "; Echo strrchr ($str," | ");? >
To run an example, output:
| Ccc
Combining the substr () function allows you to intercept everything that follows the last occurrence of a character :
<?php$str= "aaa| bbb| CCC "; Echo substr (STRRCHR ($str," | "), 1);? >