You can use strstr () and strrchr () functions to find characters in php strings. The structure of the two functions is as follows:
Strstr ($ haystack, $ needle)
Strrchr ($ haystack, $ needle)
// $ Haystack indicates the parent string, and $ needle indicates the character to be searched
The strstr () function is used to locate the first occurrence of a character and return the ending part from this position to the parent string;
The strrchr () function is used to locate the last occurrence of a character and return the part from this position to the end of the parent string.
Instance:
| The code is as follows: |
Copy code |
<? Php
$ A = "p ";
$ B = "echo is a php function, p is an html tag ";
$ Str1 = strstr ($ B, $ );
If ($ str1)
Echo $ str1. "<br> ";
Else echo "p not found". "<br> ";
$ Str2 = strrchr ($ B, $ );
If ($ str2)
Echo $ str2;
Else echo "p not found ";
?> |
The substr_count () function can be used to query the number of php string occurrences. Its structure is as follows:
Substr_count ($ haystack, $ needle [, $ offset [, $ length])
// $ Haystack indicates the parent string, and $ needl indicates the character to be searched
// $ Offset indicates the start point of the search, and $ length indicates the length of the search, all of which are optional parameters.
Instance:
| The code is as follows: |
Copy code |
|
<? Php
$ Str = "this is a test ";
Echo substr_count ($ str, 'is ').' <br> ';
Echo substr_count ($ str, 'is ', 3).' <br> ';
Echo substr_count ($ str, 'is ', 3,3).' <br> ';
?> |
You can use the strrpos () and strpos () functions to search string locations in php.
Find the last location
Use the strrpos () function. The structure is as follows:
Strrpos ($ haystack, $ needle [, $ offset])
// The parameter $ needle can only be one character, not a string. If a string is provided, only the first character in the string is taken. Other characters are invalid;
// Optional parameter $ offset sets the length of the query string.
Instance 1:
| The code is as follows: |
Copy code |
|
<? Php
$ Text = "php ";
$ Str1 = strrpos ($ text, "p ");
If ($ str1)
Echo "find the character 'P'". "<br> ";
Else echo "cannot find the character 'P'". "<br> ";
$ Str2 = strrpos ($ stxt, "q ");
If ($ str2)
Echo "find the character 'Q'". "<br> ";
Else echo "the character 'Q' is not found". "<br> ";
?> |
Locate the first time
Using the strpos () function, the strrpo () function is only one letter different from the function, but the function is quite different. The $ needle parameter of the strpos () function allows the use of a string and searches for the position of this string in $ haystack for the first time, instead of the last time.
Instance:
| The code is as follows: |
Copy code |
|
<? Php
$ Text = "one of the differences between php and asp: php is more stable and secure ";
$ Str1 = strrpos ($ text, "php ");
If ($ str1)
Echo "finds the string 'php' at: $ str1". "<br> ";
Else echo "the string 'php' cannot be found". "<br> ";
$ Str2 = strpos ($ stxt, "php ");
If ($ str2)
Echo "find the string 'php'". "<br> ";
Else echo "the string 'php' cannot be found". "<br> ";
?> |