This article mainly introduces four functions and examples of using strings in php. For more information, see
1. Obtain some strings.
The Code is as follows:
String substr (string, int start, int [length]);
This function extracts the start character of the string from the start character. If start is a negative number,
The value is counted from the end of the string. If the parameter length can be omitted, but it is a negative number, it indicates that the maximum length is obtained.
The Code is as follows:
Echo substr ("abcdef", 1, 3); // return "bcd"
Echo substr ("abcdef",-2); // return "ef"
Echo substr ("abcdef",-3, 1); // return "d"
Echo substr ("abcdef", 1,-1); // return "bcde"
2. Obtain the string starting from the last occurrence of a character.
The Code is as follows:
String strrchr (string haystack, string needle );
This function is used to find the final position of the character needle in the string haystack and
Returns a string between the ends of the haystack operation. If no needle is found, false is returned.
The Code is as follows:
$ PATH = "http: // localhost/test. php ";
$ Dir = substr (strrchr ($ PATH, ":"), 1 );
Echo $ dir;
Output: // localhost/test. php
3. Return the string from the beginning to the end of a string.
The Code is as follows:
String strstr (string haystack, string needle );
This function returns the string that needle first appears at the haystack end to the end of the haystack. If no needle is found, false is returned.
4. String comparison and parsing.
The Code is as follows:
Int ereg (string pattern, string, array [regs]);
This function uses the pattern Rule to parse and compare strings. The value returned from the comparison result is placed in the array parameter regs, the regs [0] content is the original string, regs [1] is the first regular string, regs [2] is the second regular string, and so on. If the regs parameter is omitted, only comparison is performed. If it is found, the return value is true.
The Code is as follows:
If (eregi ("^ [_/. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] + /.) + [a-z] {2, 3} $ ", $ email )){
Echo "your email has passed the preliminary check ";
}