问题
I have the following code:
explode("delimiter", $snippet);
But I want that my delimiter is case-insensitive.
回答1:
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().
回答2:
explode('delimiter',strtolower($snippet));
Never use expensive regular expressions when more CPU affordable functions are available.
Never use double-quotes unless you explicitly have a use for mixing variables inside of strings.
回答3:
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));
来源:https://stackoverflow.com/questions/12666489/php-case-insensitive-explode