- 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
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
Advertisements