PHP RegEx: Case Insensitive PHP Script

PHP RegEx: Case Insensitive PHP Script

In previous tutorial we match at the beggining of the string given. But that is a case sensitive. If your condition like this: if(preg_match(“/^ABC/”, $string)) then the value of your string “abc”. The script will returned with this message:

No match found

This is because of case sensitive. But in this script we will make the condition insensitive using regex “i” for insensitive. Shown below:



    <?php // create a string 
    $string = 'Ronard'; 
    // try to match our pattern 
    if(preg_match("/^ron/i", $string)) { 
    // echo this is it matches 
    echo 'The string begins with Ron'; 
    } else { 
    // if not match is found echo this line 
    echo 'No match found'; } 


Even if your string given are ROn, rOn, RoN the script will read insensitive and display the message “The string begins with Ron”.



Read Me


Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *