How to Check if a String Contains a Substring in PHP

Determining if a string contains a specific substring is a common task in PHP. There are several methods to achieve this, including using PHP’s built-in functions and Regular Expressions. This guide explores multiple ways to perform this operation efficiently.

Using strpos() to Check for a Substring in PHP

The strpos($string, $substring) function is a simple and widely used method to find a substring in a string. It returns the index of the first occurrence of the substring if found, or false if not. Remember, indexes in PHP start at 0, and strpos() is case-sensitive.

Example:

<?php
$string = "Hello, World!";
$substring = "World";

if (strpos($string, $substring) !== false) {
    echo "Substring found!";
} else {
    echo "Substring not found.";
}
?>

Case-Insensitive Substring Search with stripos()

If you need a case-insensitive search, use stripos($string, $substring). This function works similarly to strpos() but ignores case differences.

Example:

<?php
$string = "Hello, World!";
$substring = "world";

if (stripos($string, $substring) !== false) {
    echo "Substring found!";
} else {
    echo "Substring not found.";
}
?>

Checking for Substrings with str_contains() in PHP 8+

Starting from PHP 8, the str_contains($string, $substring) function provides a simpler way to check for substrings. It returns true if the substring exists in the string and false otherwise.

Example:

<?php
$string = "Welcome to PHP 8!";
$substring = "PHP";

if (str_contains($string, $substring)) {
    echo "Substring found!";
} else {
    echo "Substring not found.";
}
?>

Key points about str_contains():

  • It is case-sensitive.
  • It does not return the position of the substring, unlike strpos().

Using Regular Expressions for Substring Search in PHP

For more advanced substring matching, you can use Regular Expressions with the preg_match() function. This method allows flexibility, such as case-insensitive searches or matching specific patterns. However, it is slower compared to built-in functions.

Example 1: Case-Sensitive Search

<?php
$string = "PHP is powerful.";
$pattern = "/PHP/";

if (preg_match($pattern, $string)) {
    echo "Pattern matched!";
} else {
    echo "No match found.";
}
?>

Example 2: Case-Insensitive Search

<?php
$string = "PHP is powerful.";
$pattern = "/php/i";

if (preg_match($pattern, $string)) {
    echo "Pattern matched!";
} else {
    echo "No match found.";
}
?>

Example 3: Searching for Whole Words
To find specific words, use the \b anchor in your Regular Expression.

<?php
$string = "Learning PHP is fun.";
$pattern = "/\bPHP\b/";

if (preg_match($pattern, $string)) {
    echo "Word found!";
} else {
    echo "Word not found.";
}
?>

What is a String in PHP?

In PHP, a string is a sequence of characters represented by bytes. PHP supports up to 256 characters, but it doesn’t offer native Unicode support. Strings can contain alphanumeric characters and can be created using:

  • Single quotes (‘…’): These handle plain text and don’t interpret special characters.
  • Double quotes (“…”): These allow for special character interpretation (e.g., \n for a new line).

PHP provides a variety of string manipulation functions, including those for splitting, concatenation, comparison, and transformation.

Summary of Substring Search Methods

MethodCase SensitivityPHP VersionUse Case
strpos()Case-sensitiveAll versionsBasic substring search
stripos()Case-insensitiveAll versionsCase-insensitive search
str_contains()Case-sensitivePHP 8+Simplified substring search
preg_match()ConfigurableAll versionsAdvanced pattern matching

Using these methods, you can effectively check for substrings in PHP, tailoring your approach based on the complexity of your use case.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *