YogeshChauhan . com

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to Clone Objects in PHP?MiscKilling A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?MiscWhat’s new in WordPress 5.5?WordPressHow to check if the element exists using JavaScript and jQuery?JavaScriptOptional arguments, Default parameters and REST parameters in JavaScriptJavaScriptHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptHow to obfuscate JavaScript code to hide it from View Source?JavaScriptEssential SQL Commands We Need to KnowSQL/MySQLCreate a menu with a curtain falling animation using CSS and JavaScriptCSSThe substr() method in JavaScript and how it’s different from substring()JavaScriptAngular 9 time clock update every minute, second, hourAngularContainers 101: What are containers?Misc