【发布时间】:2010-09-16 19:51:00
【问题描述】:
提取括号之间的文本集的最佳/最有效方法是什么?假设我想以最有效的方式从字符串“忽略除此(文本)之外的所有内容”中获取字符串“文本”。
到目前为止,我想出的最好的是:
$fullString = "ignore everything except this (text)";
$start = strpos('(', $fullString);
$end = strlen($fullString) - strpos(')', $fullString);
$shortString = substr($fullString, $start, $end);
有没有更好的方法来做到这一点?我知道通常使用正则表达式往往效率较低,但除非我可以减少函数调用的数量,否则这可能是最好的方法?想法?
【问题讨论】:
-
您可能会发现
s($fullString)->between("(", ")")很有帮助,如在 this standalone library 中找到的。