PHP case-insensitive explode()

前端 未结 3 1042
星月不相逢
星月不相逢 2021-01-17 13:39

I have the following code:

explode(\"delimiter\", $snippet);

But I want that my delimiter is case-insensitive.

相关标签:
3条回答
  • 2021-01-17 14:23

    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().

    0 讨论(0)
  • 2021-01-17 14:34
    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.

    0 讨论(0)
  • 2021-01-17 14:38

    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));
    
    0 讨论(0)
提交回复
热议问题