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.

Syntax:


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:


<?php
$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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to make WordPress main stylesheet (style.css)?WordPressHow to get the first element with a class name xyz using JavaScript?JavaScriptEXISTS and NOT EXISTS in PostgresPostgresLEFT JOIN in PostgresPostgresControl Scrolling with CSS Scroll SnapCSSAll different methods for accessing elements in the DOM using JavaScriptJavaScriptadd_filter function in WordPressWordPressSteps to Install Microsoft SQL Server on a MacOSSQL/MySQLWhat is HTMLUnknownElement?HTMLModules and its Core features in JavaScriptJavaScriptHow to display and animate image on scroll using JavaScript?JavaScriptHow to Block IPs and User Agents using code in Drupal or WordPress?Drupal