![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements