4

How to Check If a String Contains a Specific Word in PHP

 1 year ago
source link: https://www.laravelcode.com/post/how-to-check-if-a-string-contains-a-specific-word-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Check If a String Contains a Specific Word in PHP

  1832 views

  2 years ago

PHP

Use the PHP strpos() Function

You can use the PHP strpos() function to check whether a string contains a specific word or not.

The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false. Also note that string positions start at 0, and not 1.

Let's check out an example to understand how this function basically works:

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
 
// Test if string contains the word 
if(strpos($mystring, $word) !== false){
    echo "Word Found!";
} else{
    echo "Word Not Found!";
}
?>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK