PHP String Contains
In this article, you will learn different ways to check if a string contains a substring.
During the development process, you may come to the situation where you need to check substring from a string. PHP provides many inbuilt functions to perform operations in a string. This exercise shows you different PHP functions to check whether a string contains a specified substring or not. You can use any of the following methods to achieve this.
Method 1: strstr() function
The strstr() function looks for the first occurrence of a string inside another string. This function is case-sensitive.
Syntaxstrstr(string,search,before_search)
Here, string specifies the string to search for, search specifies the string to search for and before_search is a boolean value whose default is false. If set to true, it returns the part of the string before the first occurrence of the search parameter. Both string and search are required parameters and before_search is optional.
Example<?php
$str1 = "Our mission is to provide good educational resources to technical students, learner, web developers. ";
$str2 = "mission";
$str3 = "Mission";
echo strstr($str1,$str2).'<br/>';
echo strstr($str1,$str3).'<br/>';
echo strstr($str1,$str2, true);
?>
Output
mission is to provide good educational resources to technical students, learner, web developers.
Our
Method 2: stristr() function
The stristr() function is used to search for the first occurrence of a string inside another string. The stristr works exactly the same way as the strstr does, aside from the way that it is case insensitive.
Syntaxstristr(string,search,before_search)
Example
$str1 = "Our mission is to provide good educational resources to technical students, learner, web developers. ";
$str2 = "mission";
$str3 = "Mission";
echo stristr($str1,$str2).'<br/>';
echo stristr($str1,$str3).'<br/>';
echo stristr($str1,$str2,true);
Output
mission is to provide good educational resources to technical students, learner, web developers.
mission is to provide good educational resources to technical students, learner, web developers.
Our
Method 3: str_contains() function
It determines if a string contains a given substring. It is available from PHP version 8. It performs a case-sensitive check.
str_contains( haystack, needle )
The haystack is the string to search in and the needle is the substring to search for in the haystack.
$str1 = "Our mission is to provide good educational resources to technical students, learner, web developers. ";
$str2 = "mission";
if(str_contains($str1,$str2)) {
echo "The string 'mission' is found in the string.<br/>";
}
Output
The string 'mission' is found in the string.
Related Articles
PHP String ContainsPHP reverse a string without predefined function
PHP random quote generator
PHP substr() function
PHP convert string into an array
PHP remove HTML and PHP tags from string
PHP SplFileObject Standard Library
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP