PHP Tutorial
Home
PHP Install Xampp
PHP Syntax & Comments
PHP Variables
PHP Constants
PHP Data Types
PHP Echo & Print
PHP Strings
PHP If...Else...Elseif
PHP Ternary Operator
PHP Loops
PHP Functions
PHP Arrays
PHP GET & POST
PHP Advanced
PHP Date and Time
PHP Include and Require
PHP File Upload
PHP Sessions
PHP Cookies
PHP Send Email
PHP JSON Parsing
PHP MySQL Database
PHP MySQL Introduction
PHP Connect to MySQL
PHP MySQL Create DB
PHP MySQL Create Table
PHP MySQL Insert Data
PHP MySQL Select Data
PHP MySQL Delete Data
PHP MySQL Update Data
PHP MySQL Where
Use the PHP strpos() Function
You can use the PHP strpos() function to check that a string contains a specific word or not.
The strpos() function returns the position of the first look of the substring with the string. If the substring is not found it returns false. Also note that the string positions start at 0, not 1.
Let us appearance at an example to grasp how this function works basically:
Example 1
<?php $word = "fox"; $string = "The quick brown fox jumps over the lazy dog"; // Test if string contains the word if(strpos($string , $word) !== false){ echo "Word Found!"; } else{ echo "Word Not Found!"; } ?>
Example 2
$str = "Hi, I am from howtowebcode.com"; $result = preg_match("from", $str); if($result == 1){ print_r("Yes, exists"); }