Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP case-insensitive explode()

I have the following code:

explode("delimiter", $snippet);

But I want that my delimiter is case-insensitive.

like image 870
Luis Liz Avatar asked Oct 01 '12 01:10

Luis Liz


3 Answers

Just use preg_split() and pass the flag i for case-insensitivity:

$keywords = preg_split("/your delimiter/i", $text);

Also make sure your delimiter which you pass to preg_split() doesn't cotain any sepcial regex characters. Otherwise make sure you escape them properly or use preg_quote().

like image 121
Michael Robinson Avatar answered Oct 24 '22 03:10

Michael Robinson


You can first replace the delimiter and then use explode as normal. This can be done as a fairly readable one liner like this:

explode($delimiter,str_ireplace($delimiter,$delimiter,$snippet));
like image 22
Eaten by a Grue Avatar answered Oct 24 '22 02:10

Eaten by a Grue


explode('delimiter',strtolower($snippet));
  1. Never use expensive regular expressions when more CPU affordable functions are available.

  2. Never use double-quotes unless you explicitly have a use for mixing variables inside of strings.

like image 24
John Avatar answered Oct 24 '22 03:10

John