
- 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
How to check if a string contains a specific sub string?
To check if a string contains a specific substring, the ‘strlen’ and ‘strpos’ functions can be used. The ‘strlen’ gives the entire length of the string. The function ‘strpos’ finds the position wherein the substring occurs first in the string.
Example
<?php $str_1 = "thisisasample"; $str_2 = "asample"; if (strpos($str_1, $str_2) >= 0 && strpos($str_1, $str_2) < strlen($str_1)) echo("The substring is present within the string."); else echo("The substring is not present within the string."); ?>
Output
The substring is present within the string.
Two strings are defined and the position of the second string in the first string is checked with the help of the ‘strpos’ function and compared with the length of the first string. Relevant message is displayed on the console.
- Related Articles
- Check if a string contains a sub-string in C++
- Method to check if a String contains a sub string ignoring case in Java
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- How to check if the string contains the specific word?
- How to test if a string contains specific words in Java?
- How to check if any of the strings in a column contains a specific string in MySQL?
- How to check if a Python string contains only digits?
- How to check if a string contains only decimal characters?
- How to check if a string contains a substring in Golang?
- MySQL query to check if a string contains a word?
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- How to check if a string contains a certain word in C#?
- How to check if a String contains another String in a case insensitive manner in Java?

Advertisements