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 va2s . c  om
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/*  ww  w .  j  av a 2s. co 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