How to Check If String Contains Substring in PHP

How do I check a string contains any specific word or substring in PHP. This tutorial will help you to check if a string contains a substring in the PHP programming language. For example, you have to execute a line of code only if an input string contains a specific substring. If substring found then execute further code otherwise else part will execute.

Check If String Contains a Specific Word in PHP

You can use the PHP strpos() function to check whether a string contains a specific substring or not.

The strpos() function returns the position of the first appearance of a substring in a string. If the substring is not found then it returns false.

Syntax
int strpos( $String, $Substring )

Parameter – The strpos() function accepts two parameters.

  • $String – This parameter specifies the text where searching performs.
  • $Substring – This parameter specifies the substring which we have to search.

Note: The strpos() function check string positions start from 0, and not the 1.

Example 1

The following example code snippet checks whether a specific word exists in the given string or not. It returns true because $str contains the substring “first” in it. Finally, this will print “The word ‘first’ was found”.

<?php
$str = 'This is the first paragraph of an essay.';
$word  = "first";

if (strpos($str, $word) !== false) {
    echo "The word '$word' was found";
} else {
    echo "The word '$word' was not found";
}
?>

Note – The strpos() function is used for case-sensitive comparison. However, you can use the stripos() function for case-insensitive comparison in PHP.

Example 2

The following example code snippet checks case-insensitively whether a substring exists in the given string or not. It returns true because $str contains the substring “Essay” in it. Finally, this will print The word ‘Essay’ was found.

<?php
$str = 'This is the first paragraph of an essay.';
$word  = "Essay";

if (stripos($str, $word) !== false) {
    echo "The word '$word' was found";
} else {
    echo "The word '$word' was not found";
}
?>

Example 3

In the following example, you will see the substring exists at the beginning position in the given string or not. However, it returns true because $str contains the substring “This” at the start position. Thus, this will print The word ‘This’ was found at the beginning.

<?php
$str = 'This is the first paragraph of an essay.';
$word  = "This";

if (strpos($str, $word) === 0) {
    echo "The word '$word' was found at the beginning";
} else {
    echo "The word '$word' was not found at the beginning";
}
?>

We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other PHP related articles.

Icon made by Yannick Lung

If you like our article, please consider buying a coffee for us.
Thanks for your support!

Support us on Buy me a coffee! Buy me a coffee!



Join the Discussion.