YogeshChauhan.com
Check If a String Contains a Specific Word in PHP
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
}
?>
dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

Crypto.getRandomValues() method in JavaScriptJavaScriptSocial Media Colors: LESS VariablesMiscHow to add multiple Columns in CSS like Bootstrap?CSSLearn how to use Self JOIN in SQL and MySQLSQL/MySQLLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPHow to Make a Simple Module with a Form and Menu Link in Drupal 7.x?DrupalArray destructuring and Object destructuring in JavaScriptJavaScriptQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQLJavaScript Input Validation Theme Park Example using throw StatementJavaScriptbin2hex() and chr() String Functions in PHPPHPThe flex-grow, flex-shrink and flex-basis Properties in CSSCSSLearn to create your skill bar using CSSCSS