
- 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 to check substring in a string
To check substring in a string, the code is as follows in PHP −
Example
<?php $subStr = "Mother"; $str = "How I Met Your Mother"; echo "String = $str"; echo "
Substring = $subStr"; if(strpos($str, $subStr) !== false){ echo "
Substring found successfully"; } else{ echo "
Substring not found"; } ?>
Output
This will produce the following output−
String = How I Met Your Mother Substring = Mother Substring found successfully
Example
Let us now see another example −
<?php $subStr = "Ocean"; $str = "In the Heart of Sea"; echo "String = $str"; echo "
Substring = $subStr"; if(strpos($str, $subStr) !== false){ echo "
Substring found successfully"; } else{ echo "
Substring not found"; } ?>
Output
This will produce the following output−
String = In the Heart of Sea Substring = Ocean Substring not found
- Related Articles
- PHP 8 – Using str_contains() to check if a string contains a substring
- How to check if string or a substring of string starts with substring in Python?
- 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 if a string contains a substring in Golang?
- Check if a String Contains a Substring in Linux
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- Check if substring present in string in Python
- How to check whether a String contains a substring or not?
- Golang Program to check a string starts with a specified substring
- Swift Program to check a string starts with a specified substring
- Swift Program to check a string end with a specified substring
- Java program to check if a Substring is present in a given String
- C# program to check if a Substring is present in a Given String

Advertisements