Horje
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 geeks for geeks ";

//sub string to be checked
$sub = "geeks";

// 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 geeks for geeks ";

//sub string to be checked
$sub = "computer";

// Test if string contains the word
if (strpos($input_string, $sub) !== false)
{
    echo "True";
}
else
{
    echo "False";
}
?>
[/GFGTABS]

Output:

False

Example 3: The following example also considers space.

PHP
<?php
//input string
$input_string = "geeks for geeks 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 = "geeks for geeks 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

Using substr_count function

The substr_count function in PHP efficiently counts the occurrences of a substring within a string. It returns the total count of occurrences, allowing for simple checks of substring presence or frequency within the given string.

Example:

PHP
<?php
$string = "Hello World";
$substring = "World";

$count = substr_count($string, $substring);

if ($count > 0) {
    echo "Substring found";
} else {
    echo "Substring not found";
}
?>

Output
Substring found

Using preg_match Function

The preg_match function in PHP is used to perform a regular expression match. It returns the number of times the pattern matches (which will be 1 if it matches and 0 if it doesn’t).

Example:

PHP
<?php
//input string
$input_string = "sravan kumar author at geeks for geeks ";

//pattern to be checked
$pattern = "/geeks/";

// Test if string contains the pattern
if (preg_match($pattern, $input_string)) {
    echo "True";
} else {
    echo "False";
}
?>

Output
True






Reffered: https://www.geeksforgeeks.org


PHP

Related
AsgardCMS - Introduction of in-built and ready to use modules AsgardCMS - Introduction of in-built and ready to use modules
PHP SplPriorityQueue extract() Function PHP SplPriorityQueue extract() Function
PHP SplPriorityQueue current() Function PHP SplPriorityQueue current() Function
PHP SplPriorityQueue count() Function PHP SplPriorityQueue count() Function
PHP SplPriorityQueue compare() Function PHP SplPriorityQueue compare() Function

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8