Categories
PHP

Find a given substring in the string

How to use the str_contains function to check if a string contains a given substring, str_starts_with function to check if a string starts with a given substring, and str_ends_with function to check if a string ends with a given substring.

  1. str_contains: Check if a string contains a given substring
  2. str_starts_with: Checks if a string starts with a given substring
  3. str_ends_with: Checks if a string ends with a given substring

Note: These functions are available from PHP 8 onwards, see related functions for previous versions here: Working with Substrings.

str_contains: Check if a string contains a substring

<?php
 //Syntax
 str_contains(string $haystack, string $needle): bool

The str_contains function was introduced in PHP 8, prior to PHP 8 there was not a specific PHP function that tells you whether or not a given substring exists within a string, coders mostly used the strpos() function to determine whether a substring exists in the string or not. The str_contains function accepts two arguments:

  1. $haystack the string to search in and
  2. $needle the substring to search for in the haystack.

This function returns true if the given string $needle is available in the $haystack string and returns false if the $needle is not found in the $haystack string.

<?php
 $haystack = 'My website name is BrainBell.com';
 $needle   = 'BrainBell';
 $exist    = str_contains($haystack, $needle);
 if ($exist)
  echo 'The word "BrainBell" availble in $haystack';
 else
  echo 'Not found';

 //Prints: The word "BrainBell" availble in $haystack

Example: This function is case-sensitive, so the following example returns false:

<?php
 if (str_contains('BrainBell.com', 'brainbell'))
  echo 'The word "BrainBell" availble in $haystack';
 else
  echo 'Not found';

 //Prints: Not found

Example: Empty stirng

PHP 8 considers that an empty substring exists at every position in a string, so checking an empty string with str_contains() will always return true. See the following code:

<?php
 //true
 if (str_contains('', ''));
  echo 'both haystack and needle are empty';

 //true
 if (str_contains('BrainBell.com', ''))
  echo 'haystack contains an empty substring';

Example: Create str_contains() function for older versions of PHP

If you are writing a script that contains the str_contains() function and you want to ensure that your code must run on versions prior to PHP 8 then create your own str_contains() function:

<?php
 if (!function_exists('str_contains')){
  function str_contains($haystack, $needle) {
  return $needle === '' || strpos($haystack,$needle) !== false;
 }}

str_starts_with: Check if a string starts with a specific substring

<?php
 //Syntax
 str_starts_with(string $haystack, string $needle): bool

The str_starts_with function works similar to the str_contains function but its searches for the substring $needle from the beginning of the string $haystack.

<?php
 $haystack = 'My website name is BrainBell.com';
 $needle   = 'My website';
 //true
 if (str_starts_with ($haystack, $needle))
  echo 'Found';
 //Prints: Found

Example: Empty stirng

Similar to str_contains function, this function is also case-sensitive and always returns true when searching an empty substring.

<?php
 //Always prints
 if (str_starts_with('', ''))
  echo 'both haystack and needle are empty';

 //Always prints
 if (str_starts_with('BrainBell.com', ''))
  echo 'BrainBell.com starts with an empty substring';

Exmaple: str_starts_with() bckward compatibility with older PHP versions

In the example “Create str_contains() function for older versions of PHP” we created the str_contains() function for compatibility reasons. In following code we used the same technique to create the str_starts_with() function for older PHP versions:

<?php
 if (!function_exists('str_starts_with')){
  function str_starts_with($haystack, $needle) {
  return $needle === '' || strpos($haystack,$needle) === 0;
 }}

str_ends_with: Check if a string ends with a specific substring

<?php
 //Syntax
 str_ends_with(string $haystack, string $needle): bool

This function works similar to the str_starts_with and the str_contains functions, except that it searches for the substring $needle at the end of the string $haystack.

<?php
 $haystack = 'My website name is BrainBell.com';
 $needle   = 'com';

 //always prints
 if (str_ends_with($haystack, $needle))
  echo 'The $haystack string ends with "com" word';

 //Case not match, never prints
 if (str_ends_with($haystack, 'COM'))
  echo 'The $haystack string ends with "com" word';

 //always prints
 if (str_ends_with('', ''))
  echo 'both haystack and needle are empty';

 //always prints
 if (str_ends_with($haystack, ''))
  echo 'needle is empty';

Example: str_ends_with backward compatibility

<?php
 if (!function_exists('str_ends_with')){
  function str_ends_with($haystack, $needle) {
  return $needle === '' || 
         $needle === substr($haystack,-(strlen($needle)));
 }}

Perform case-insensitive check

The str_contains, str_starts_with, and str_ends_with functions are case-sensitive and their case-insensitive equivalent functions are not available. But you can perform a case-insensitive operation by lowering the case of both $haystack and $needle, see the following example:

<?php
 $haystack = 'My website name is BrainBell.com';
 $needle   = 'Brainbell';
 $exist    = str_contains(
                 strtolower($haystack),
                 strtolower($needle)
              );
 var_dump($exist);
 //bool(true)

Manipulating substrings: