
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP 8 – Using str_contains() to check if a string contains a substring
In PHP 8, str_contains function determines if a string contains a given substring anywhere. The str_contains function checks if a first-string is contained in the second string and it returns a true /false Boolean value based on whether the string is found or not. it is a self-explanatory function.
str_contains(string $haystack, string $needle): bool
Example1 : PHP 8 str_contains function.
<?php if (str_contains('great reading tutorial', 'tutorial')) { var_dump('Tutorial has been found'); } ?>
Output
string(23) "Tutorial has been found"
Example: str_contains function.
<?php if (str_contains('great reading tutorial', 'hello')){ var_dump('great reading'); } ?>
Note: The above program returns false because the first string does not contain the second string.
strpos() function
In PHP 7.x, strops() function is used to check if a given string contains another string or not. This function returns the position of the needle string, or it returns false if the string needle is not found.
if (strpos('string with lots of words', 'words') !== false) { /* … */ }
Example: PHP 7.x strops() function
<?php $string = 'Hello World!'; if (strpos($string, 'Hello') !== false) { echo 'True'; } ?>
Output
True
- Related Articles
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- Check if a String Contains a Substring in Linux
- How to check if a string contains a substring in Golang?
- How to check if an input string contains a specified substring in JSP?
- Check if a string contains a number using Java.
- How to check whether a string contains a substring in JavaScript?
- How to check whether a string contains a substring in jQuery?
- How to check whether a String contains a substring or not?
- MySQL query to check if a string contains a value (substring) within the same row?
- How do we check if a String contains a substring (ignoring case) in Java?
- Golang Program to check a string contains a specified substring or not
- How to check if a string contains a specific sub string?
- Check if a string contains a sub-string in C++
- MySQL query to check if a string contains a word?

Advertisements