Friday, October 10, 2025
HomeLanguagesHow to check if a String Contains a Substring in PHP ?

How to check if a String Contains a Substring in PHP ?

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
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS