0

Checking for a String using PHP

To detect whether a string is part of another string, use strstr. This function takes two parameters: the string to search through and the string to search for. It is not case sensitive; if you want to use a function that is case sensitive, use stristr. Lastly, there is strops, which finds the position of every first occurrence of the string you specified.

Detecting whether a string is contained in another string

$password="secretpassword1";

if (strstr($password,"password")){
echo('Passwords cannot contain the word "password".');
}
else {
echo ("Password accepted.");
}
?>

This Code Returns: Passwords cannot contain the word “password”.

Filed in: PHP Functions

Recent Posts

Bookmark and Promote!

© 2012 Sumit Bansal. All rights reserved. XHTML / CSS Valid.