我想通過一系列字詞拆分大字符串。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