Using PHP Str_Contains for Efficient String Search

This tutorial provides a comprehensive guide to the `str_contains()` function in PHP. This function is a powerful tool for developers, enabling them to check if a particular substring exists within a given string.

Introduction to PHP str_contains() Function

The `str_contains()` function is an integral part of PHP’s string manipulation capabilities. It is designed to search for the presence of a substring within a string. The function is straightforward in its usage and follows this syntax: 

`str_contains ( string $haystack , string $needle ) : bool`.

Parameters Explained

  • `$haystack`: This parameter is the string in which the search is performed;
  • `$needle`: This refers to the substring that needs to be located within the `$haystack`.

Functionality and Return Values

The function returns a boolean value: `true` if the `$needle` is found within the `$haystack` and `false` if it is not. A key aspect of the `str_contains()` function is its case-sensitive approach to searching, which plays a crucial role in its operation.

Availability Across PHP Versions

It’s important to note that the `str_contains()` function was introduced in PHP version 8.0.0. Users of prior PHP versions can create a similar functionality using a polyfill method:

```php
if (!function_exists('str_contains')) {
    function str_contains( $haystack, $needle)
    {
        return $needle !== '' && mb_strpos($haystack, $needle) !== false;
    }
}
```

This code snippet provides a similar functionality to `str_contains()` for those using PHP versions earlier than 8.0.0.

Practical Application and Examples

The following examples demonstrate the practical use of the `str_contains()` function in various scenarios.

Example 1: Basic String Search

This example shows a basic application of the function:

```php
$haystack = 'PHP is cool.';
$needle = 'PHP';

$result = str_contains($haystack, $needle) ? 'is' : 'is not';

echo "The string {$needle} {$result} in the sentence.";
```

Output:

`The string PHP is in the sentence.`

Example 2: Demonstrating Case Sensitivity

The case-sensitive nature of the `str_contains()` function is illustrated here:

```php
$haystack = 'PHP is cool.';
$needle = 'Cool';

$result = str_contains($haystack, $needle) ? 'is' : 'is not';

echo "The string {$needle} {$result} in the sentence.";
```

Output:

`The string Cool is not in the sentence.`

Conclusion: Importance of str_contains() in PHP

The `str_contains()` function is a vital tool in PHP programming for string manipulation and searching. Its introduction in PHP 8.0.0 has simplified the process of detecting the presence of substrings within a larger string. The function’s case sensitivity adds to its precision, making it a reliable choice for developers who need to perform exact string matching operations. Its ease of use, combined with the powerful functionality it offers, makes it an indispensable part of PHP’s string manipulation toolkit.