php – 使用preg_replace截断字符串的最有效方法?

我正在查看一些代码并开始考虑使用preg_replace截断字符串(在本例中为URI)的最有效方法.

首先 – 我意识到首先使用preg_replace对于这个任务来说可能是过度的,它可能是不必要的昂贵,并且最好使用PHP的字符串友好函数(如substr)来处理它.我知道这个.

也就是说,考虑这两种不同的正则表达式:

$uri = '/one/cool/uri';    // Desired result '/one/cool'

// Using a back-reference
$parent = preg_replace('#(.*)/.*#', "$1", $uri);

// Using character class negation
$parent = preg_replace('#/[^/]+$#', '', $uri);

默认情况下,我认为在前一种情况下,创建后向引用将比不这样做更昂贵,因此第二个示例将更可取.但后来我开始想知道在第二个例子中使用[^ /]是否比相应的更昂贵.在第一个例子中,如果是这样,还有多少?

从可读性的角度来看,我更喜欢第一个例子,因为我们分裂了头发,所以我倾向于在两者之间选择它(毕竟,编写可读代码也很有价值).可能只是我个人的偏好.

思考?

最佳答案 我也会测量两种选择的运行时间.来自文档的这些信息也可能有所帮助:

http://www.php.net/manual/en/regexp.reference.performance.php

If you are using such a pattern with subject strings that do not contain newlines, the best performance is obtained by setting PCRE_DOTALL, or starting the pattern with ^.* to indicate explicit anchoring. That saves PCRE from having to scan along the subject looking for a newline to restart at.

所以,$parent = preg_replace(‘#^(.*)/.*#s’,“$1”,$uri);可能会加速第一种选择.第二个不需要这个设置:

s (PCRE_DOTALL)

If this modifier is set, a dot metacharacter in the
pattern matches all characters, including newlines. Without it,
newlines are excluded. This modifier is equivalent to Perl’s /s
modifier. A negative class such as [^a] always matches a newline
character
, independent of the setting of this modifier.

点赞