How to check if php strings contains other strings or specific word

Many are the times that you always need to check if a certain string is contained in another string or a specific string contains a specific word. PHP has some inbuilt functions that will enable us understand if a string contains other strings or certain words. Under this walk through we are going to understand how to check if String contains other strings or words in PHP

How to check if string contains other strings or certain words in PHP

strpos() inbuilt function will enable us find occurrence of one string inside another.

Also Read: How to detect http links in the comments using php

$str = "I love programming";

if(strpos($str, "programming") !== false ){
echo "programming string was found";
} else {
echo "There was no programming string found";

In our example code above, we have a string “I love programming”, we went ahead and checked if string “programming is contained in it”. That’s how to check if a string is contained in another string using PHP

Also Read: SQL PDO, how to use LIKE CLAUSE in PHP with PDO

How to validate email in PHP using strpos() function

Having seen how to detect if certain words or strings are contained in another string, we can comfortably use the same concept to validate email in PHP, without using javascript. This is how to do it


$email = $_POST[’email’];

if(strpos($email, "@") === false || strpos($email, ".") === false){
echo "Invalid email";
} else {
// this means email is valid proceed with your work
echo "Email address is valid";


That’s exactly how to check if a string is contained in another string or contains another word in PHP. If this tutorial helped you, you can leave a cheers message in the commenting system below.

AdBlock Detected!
This site depends on revenue from ad impressions to survive. If you find this site valuable, please consider disabling your ad blocker or pausing Adblock for this website.