Want to know whether a string contains a specific substring. For example, you want to check whether an email address contains @. Problem
Want to know whether a string contains a specific substring. For example, you want to check whether an email address contains @.
Solution
If (strpos ($ _ POST ['email '],' @ ') === false) {echo 'there was no @ in the e-mail address! ';}Discussion
-
The return value of strpos () is the first position of the substring in the string.
-
If no substring exists in the string, strpos () returns false.
-
If the substring is at the starting position of the string, strpos () returns 0 because position 0 indicates the starting position of the string.
-
To distinguish between 0 and false, the constant operator (=) or non-constant operator (! =)
-
In the preceding example, use = to compare the return value of strpos () with false. This test succeeds only when strpos () returns false. if strpos () returns 0 or any other number, the test fails.
For more [PHP] Strings-for articles about accessing substrings, refer to the PHP Chinese website!