Home >Backend Development >PHP Problem >How to determine if a string does not contain a certain character in php

How to determine if a string does not contain a certain character in php

藏色散人
藏色散人Original
2021-07-10 09:26:513250browse

In PHP, you can use the strpos() function to determine that a string does not contain another string. The implementation code is "if (strpos($a, 'are') !== false) {. ..} else {...}".

How to determine if a string does not contain a certain character in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php determine that a string does not contain a certain character? Determine whether a string contains another string?

We can use the strpos() function to determine whether the string contains another string:

$a = 'How are you?';
if (strpos($a, 'are') !== false) {
    echo 'true';
} else {
    echo 'false';
}

Note that you must use !== false to make the judgment conditional statement, strpos( ) If the string is not found, it returns FALSE. If another string is found at the beginning of the string, it returns 0. Other positions are integers greater than 0, so we cannot use code like this!strpos( $a, 'are'), this will lead to misjudgment.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine if a string does not contain a certain character in php. For more information, please follow other related articles on 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