PHP - String case sensitive search

Introduction

Search and replacement functions can be case-sensitive and case-insensitive.

Demo

<?php

$myString = "Hello, world!";

// Displays "Not found"
if ( strstr( $myString, "hello" ) )
   echo "Found";/*from   w w  w  . j  a  v a  2s.c  o  m*/
else
   echo "Not found";
?>

Result

PHP includes case-insensitive versions of many string functions, and they'll work even if the case of the strings don't match.

For example, there's a case-insensitive version of strstr(), called stristr():

Demo

<?php

$myString = "Hello, world!";

// Displays "Found"
if ( stristr( $myString, "hello" ) )
     echo "Found";
else/*w ww .  j  a  va 2 s . c  o  m*/
     echo "Not found";
?>

Result

Here's a list of case-insensitive string functions:

Function Case-Insensitive Equivalent
strstr() stristr()
strpos() stripos()
strrpos() strripos()
str_replace() str_ireplace()

Related Topic