我想用一系列单词分割一个大字符串。
E.g.
$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 http://php.net/preg_split最好的方法,或者有更有效的方法吗?我希望它尽可能快,因为我将分割数百 MB 的文件。
这应该是相当有效的。但是,您可能想使用一些文件进行测试并报告性能。
$splitby = array('these','are','the','words','to','split','by');
$text = 'This is the string which needs to be split by the above words.';
$pattern = '/\s?'.implode($splitby, '\s?|\s?').'\s?/';
$result = preg_split($pattern, $text, -1, PREG_SPLIT_NO_EMPTY);
- 正则表达式演示:http://rubular.com/r/jNUO1KvrXg http://rubular.com/r/jNUO1KvrXg
- PHP 代码演示:http://www.ideone.com/ov3Wl http://www.ideone.com/ov3Wl
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)