我想通过一系列字词拆分大字符串。PHP preg_split:将字符串拆分为其他字符串
例如
$splitby = array('these','are','the','words','to','split','by');
$text = 'This is the string which needs to be split by the above words.';
那么结果将是:
$text[0]='This is';
$text[1]='string which needs';
$text[2]='be';
$text[3]='above';
$text[4]='.';
我怎样才能做到这一点?是preg_split
最好的方法,还是有更高效的方法?我希望它尽可能快,因为我将分割数百MB的文件。
Afternote:racar的答案是最快的,如果array_flip在$ splitby上执行,然后使用isset()而不是in_array()。 preg_split不起作用,因为$ splitby中有数百个单词。 – Alasdair