by Sai gowtham

How to check whether a string contains a specific word in PHP

To check whether a string contains a specific word or not, we can use the strpos() function in PHP.

$str = 'i love apples';

$word = 'love'

if (strpos($str, $word) !== false) {
 echo 'word is found';
}else{
 echo 'word not found'
}

Note: The strpos() function, returns the position of the first occurrence of a substring in a string else it returns false if a substring is not found.

String positions starts from 0.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course