PHP String Contains a Specific Word or Substring

php string contains

PHP string contains : In this tutorial we will see how to check that a string contains a specific character or substring.

Introduction

A string is a sequence of characters, which can be a variable or a literal constant in PHP or as in most programming languages. A specific part of a string is called a substring.

In PHP, there are several ways to check if a character, word or substring is included or not in another string. Here are the different methods we will discuss in this tutorial :

  • The strpos() function to check if the string contains a substring. This function is case sensitive.
  • The stripos() function to check if the chain contains a substring. This function is case insensitive.
  • Using strstr() to check if the string contains a substring

Let’s review these different methods with a few examples of their use.

PHP String Contains

Using strpos() to Check if String Contains a Specific Substring

The easiest way to check if a string contains a specific sub-string is to use the strpos() method:

# Syntax function

strpos(string,substring,offset)

This function can take up to 3 parameters:

  • String: This parameter specifies the text in which the search is performed.
  • Substring: This parameter specifies the substring we should search for.
  • Offset (Optional): Search will start with this number of characters counted from the beginning of the string. If this number is negative, the search will start from the end of the string.

The function returns True if the word or substring was found in the string and False if not.

Example 1

The following code will return True because the main string $str contains the substring “Amira”.

$str = 'Amiradata programming and datascience';
 
if (strpos($str, 'Amira') !== false) {
    echo 'true';
}
else {
    echo 'false';
}

Example 2

The following code will return False because the strpos() function is case sensitive. The substring must exactly match the value you are looking for.

$str = 'Amiradata programming and datascience';

if (strpos($str, 'amira') !== false) {
    echo 'true';
}
else {
    echo 'false';
}

Example 3

The following code will return False since the substring starts at position 0.

$str = 'Amiradata programming and datascience';

if (strpos($str, 'Amira',2) !== false) {
    echo 'true';
}
else {
    echo 'false';
}

Using stripos() to Check if PHP String Contains a Specific Substring

The main disadvantage of the strpos() function is that it is case sensitive. If you don’t care about the case of the searched substring, you can use the stripos() function. The only difference with the strpos() function is that this function is case insensitive.

Here is its syntax :

stripos(string,substring,offset)

This function also takes 3 parameters :

  • String: This parameter specifies the text in which the search is performed.
  • Substring: This parameter specifies the substring we should search for.
  • Offset (Optional)

It returns True if the substring was found in the main string and False if not.

Example 1

The code below returns True, the substring is indeed present in the main string

$str = 'Amiradata programming and datascience';
 
if (stripos($str, 'Amira') !== false) {
    echo 'true';
}
else {
    echo 'false';
}

Example 2

The code below returns True, the function is case insensitive and the substring is present in the main string.

if (stripos($str, 'amira') !== false) {
    echo 'true';
}
else {
    echo 'false';
}

Using strstr() to Check if PHP String Contains a Specific Substring

The strstr() function works on the same concept as the strpos() function. It also has a case-insensitive equivalent called stristr().

Example 1

This function returns True, the substring is present (Case sensitive)

if (strstr($str, 'Amira') !== false) {
    echo 'true';
}
else {
    echo 'false';
}

Example 2

This function returns True, the sub-string is present (Case Insensitive)

if (stristr($str, 'amira') !== false) {
echo 'true';
}
else {
echo 'false';
}

Conclusion

We have seen in this tutorial that there are several ways to check if a word or a substring is present or not in another string.

The easiest way is to use the strpos() function if you want the substring to be case sensitive. Otherwise it is better to use the stripos() function.

I hope this tutorial has interested you, don’t hesitate to tell me in comments if you have any questions on the subject.

See you soon,

Back to the PHP menu

Published
Categorized as PHP

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

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