This article will introduce you to the method of checking whether a string contains substrings in PHP. For example, a specific line of code can only be run if any input string contains another substring, as we'll see below.
Let’s look at a specific example
Example 1:
The value of the following code will be true, Because the main string $str contains the substring "this". This will output "true".
<?How to check if a string contains a substring in PHP $str = 'This is Main String'; if (strpos($str, 'This') !== false) { echo 'true'; } ?>
Example 2:
The value of the following code will be false because the main string $str does not contain the substring "hello". There will be no output.
<?How to check if a string contains a substring in PHP $str = 'This is Main String'; $substr = "Hello"; if (strpos($str, $substr) !== false) { echo 'true'; } ?>
Example 3: String contains substring at the beginning
The following code will check whether the string is concatenated with the substring at the beginning. The value of the following code will be true because the main string $str contains the substring "this" at the beginning.
<?How to check if a string contains a substring in PHP $str = 'This is Main String'; if (strpos($str, 'This') === 0 ) { echo 'true'; } ?>
This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the How to check if a string contains a substring in PHP Chinese website! ! !
The above is the detailed content of How to check if a string contains a substring in PHP. For more information, please follow other related articles on the PHP Chinese website!