php

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

In this tutorial, we are going to see how to check if a string contains a specific word in PHP. You can use the PHP strpos() function to check if a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a word in a string. If the word is not found, it returns false. Also, note that the position starts at 0 and not at 1.
 

How to Check If a String Contains a Specific Word in PHP
<?php
	$word = "StackHowTo";
	$str = "Hello, Welcom to StackHowTo";
	 
	// Check if the string contains the word
	if(strpos($str, $word) !== false){
		echo "The word exists!";
	} else{
		echo "The word does not exist!";
	}
?>

Output:

The word exists!
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More [st_adsense]

Leave a Reply

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