Search code examples
phpstringexplodecase-insensitive

How to explode a string ignoring the case of the delimiter?


I have the following code:

$string = "zero Or one OR two or three";
print_r(explode("or", $string));

Right now this results in:

Array ( [0] => zero Or one OR two [1] => three ) 

But I want to ignore the case of the delimiter so it works with Or, OR, ... and that my result is:

Array ( [0] => zero [1] => one [2] => two [3] => three ) 

How can I do this?


Solution

  • Use preg_split()

    $string = "zero Or one OR two or three";
    $keywords = preg_split("/or/i", $string);
    echo '<pre>';print_r($keywords);echo '</pre>';
    

    Outputs:

    Array
    (
        [0] => zero 
        [1] =>  one 
        [2] =>  two 
        [3] =>  three
    )