Free UI Resources - UI Dev Made Easy
FAQ

How to check if some String Contains a Specific Word in PHP

We can use PHP strpos() function for this case. The strpos() work finds the position of the primary occurrence of a string inside another string. Note: The strpos() work is case-sensitive.

(PHP 4, PHP 5, PHP 7, PHP 8)

USAGE

<?php
$searchWord= "Microsoft";
$searchIn= "People use random text in Microsoft Word to act as a placeholder for inserting more sensible text later on.";
 
// Check if string Contains a Specific Word
if(strpos($mystring, $word) !== false){
    echo "Microsoft word found!";
} else{
    echo "Microsoft word not found!";
}
?>

Note: strpos() function is case-sensitive

  • stripos()  – Case INSENSITIVE – Finds the position of the FIRST occurrence of a string inside another string
  • strripos() – – Case INSENSITIVE – Finds the position of the LAST occurrence of a string inside another string
  • strrpos() – Case Sensitive – Finds the position of the LAST occurrence of a string inside another string

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top