A string is a collection of given characters and a substring is a string present in a given string.
In this article, we are going to check if the given string contains a substring by using the PHP strpos() function.
Syntax:
strpos($original_string,$sub_string);
Parameters:
- original_string : The input string
- sub_string : The substring searched in the original input string.
Return type: Boolean value
- True, If substring found
- False, If substring not found.
Example:
PHP
<?php //input string $input_string = "sravan kumar author at neveropen for neveropen "; Â Â //sub string to be checked $sub = "neveropen"; Â Â // Test if string contains the word if (strpos($input_string, $sub) !== false) { Â Â Â Â echo "True"; } else{ Â Â Â Â echo "False"; } ?> |
Output:
True
Example 2:
PHP
<?php //input string $input_string = "sravan kumar author at neveropen for neveropen "; Â Â //sub string to be checked $sub = "computer"; Â Â // Test if string contains the word if (strpos($input_string, $sub) !== false) { Â Â Â Â echo "True"; } else{ Â Â Â Â echo "False"; } ?> |
Output:
False
Example 3: The following example also considers space.
PHP
<?php //input string $input_string = "neveropen for neveropen java python php"; Â Â //sub string to be checked $sub = " "; Â Â // Test if string contains the word if (strpos($input_string, $sub) !== false) { Â Â Â Â echo "True"; } else{ Â Â Â Â echo "False"; } ?> |
Output:
True
Example 4:
PHP
<?php //input string $input_string = "neveropen for neveropen java python php"; Â Â //sub string to be checked $sub = " dbms"; Â Â // Test if string contains the word if (strpos($input_string, $sub) !== false) { Â Â Â Â echo "True"; } else{ Â Â Â Â echo "False"; } ?> |
Output:
False
