Home  >  Article  >  Backend Development  >  [PHP] String - access substring

[PHP] String - access substring

高洛峰
高洛峰Original
2017-02-18 16:35:271178browse

Question

Want to know if a string contains a specific substring. For example, you want to see if an email address contains an @ .

Solution

if(strpos($_POST['email'],'@') === false) {    
echo 'There was no @ in the e-mail address!';
}

Discussion

  1. The return value of strpos() is the first position where the substring appears in the string

  2. If there is no substring in the string, strpos() will return false

  3. ##If the substring is located at the beginning of this string, strpos() will return 0 because position 0 represents the start of the string.

  4. In order to distinguish between 0 and false, you must use the identity operator (===) or the non-identity operator (!==)

  5. In the above example, === is used to compare the return value of strpos() with false. This test will only succeed if strpos() returns false. If strpos() returns 0 or any other number, the test will not succeed.


For more [PHP] string-access substring related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn