加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:19:35 所属栏目:PHP教程 来源:网络整理
导读:我正在查看一些代码并开始考虑使用preg_replace截断字符串(在本例中为URI)的最有效方法. 首先 – 我意识到首先使用preg_replace对于这个任务来说可能是过度的,它可能是不必要的昂贵,并且最好使用PHP的字符串友好函数(如substr)来处理它.我知道这个. 也就是说
我正在查看一些代码并开始考虑使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读