Yogesh Chauhan's Blog

Check If a String Contains a Specific Word in PHP

in PHP on May 6, 2020

We can use the PHP strpos() Function to do that.

The strpos() function finds the position of the first occurrence of a string inside another string.


strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int

In simple terms,

strpos(the string to loon into, the string to find, start point of search)

First two parameters are required and third one is optional.

Example to search a word:

$word = "Yogesh";
$searchInThisString = "This website is Yogesh Chauhan's Blog";
if(strpos($searchInThisString, $word) !== false){
    echo "word found";
    //add more logic if you want
} else{
    echo "word NOT found";
    //add more logic if you want

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What happens when we add Numbers and Strings in JavaScript?JavaScriptSQL Right JoinSQL/MySQLHow to change style of all direct children of an element using jQuery?jQueryWhat is the difference between let and var in Swift?SwiftHow to remove WordPress TinyMCE Editor buttons?WordPressNATURAL JOIN in PostgresPostgresA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSHow to create a pricing table using flex in CSS?CSSLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPHow to compile and watch Sass using Gulp in WordPress?SCSSHow to use @each rule with key-value pair in SCSS?SCSSHow to overwrite file contents with new content in PHP?PHP