今天从v2ex的一个链接上看到的, 使用preg_split分割字符串,
尤其在分割tags的时候, 很不错.
Ever use explode() and then end up looping through the results and picking out the empty ones? Isn’t that annoying? By using preg_split this tedious process can be avoided:
// instead of... $str = "a,b,,c,d" explode(",", $str); // array('a','b','','c','d')// do... preg_split("~,~", $str, -1, PREG_SPLIT_NO_EMPTY); // array('a','b','c','d')The above example might not seem all that useful so consider bb code parsers. One of the major problems with parsing bb code is that most of the bbcode to html translators don’t fix improper closing tags, or even check for them! Generally speaking, most bb code parsers will to a flat preg_replace for each of the tags.