Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

Many times you need to check if a certain string is contained in another string or if a specific string contains a specific word. PHP has some inbuilt functions that will enable us to understand if a string contains other strings or certain words. Under this walkthrough, we are going to understand how to check if a 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 to find an occurrence of one string inside another.

[code]
$str = “I love programming”;

if(strpos($str, “programming”) !== false ){
echo “programming string was found”;
} else {
echo “There was no programming string found”;
}
[/code]

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

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

[code]

$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”;
}
[/code]

Conclusion

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



This post first appeared on WPBlogCafe, please read the originial post: here

Share the post

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

×

Subscribe to Wpblogcafe

Get updates delivered right to your inbox!

Thank you for your subscription

×