PHP case-insensitive explode()

拥有回忆 提交于 2019-12-01 14:18:33

问题


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




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!