gpt4 book ai didi

PHP preg_split : Split string by other strings

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:58:12 26 4
gpt4 key购买 nike

我想用一系列单词拆分一个大字符串。

例如

$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 的文件。

最佳答案

这应该是相当有效的。但是,您可能想要测试一些文件并报告性能。

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

关于PHP preg_split : Split string by other strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8074545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com