[PHP] String - access substring

高洛峰
Release: 2023-03-05 20:20:01
Original
1206 people have browsed it

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!';
}
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!