PHP字符串函数或PHP中的正则表达式哪个更有效? [英] Which is more efficient, PHP string functions or regex in PHP?

查看:119
本文介绍了PHP字符串函数或PHP中的正则表达式哪个更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写PHP代码以解析字符串.它需要尽可能快,所以正则表达式是可行的方式吗?我有一个预感,PHP字符串函数更昂贵,但这只是一个猜测.真相是什么?

以下是我需要对字符串进行的具体操作:

抓取前半部分(基于子字符串"000000"的第三个位置),并将其哈希值与接下来的20个字节进行比较,丢弃所有剩余的内容.

将第9个字节通过下一个"000000"解析为一条数据.然后在那之后抓取接下来的19个字节,并将其分成8个(折腾1)和8个.然后我做一些其他的事情,将这两个8字节的字符串转换为日期.

这就是我需要做的事情.

解决方案

这取决于您的情况:如果您尝试做一些相当基本的事情(例如:搜索字符串,用其他东西替换子字符串),则常规的字符串函数是必经之路.如果您想做一些更复杂的事情(例如:搜索IP地址),那么Regex函数绝对是一个更好的选择.

我没有分析过正则表达式,所以我不能说它们在运行时会更快,但是我可以告诉你,花一些额外的时间使用基本功能来破解等效正则表达式是不值得的./p>


在OP中使用新信息进行

这听起来好像您实际上需要在这里进行一些小的字符串操作.由于每个单独的步骤都是非常基本的,并且我怀疑您是否可以使用正则表达式一次完成所有这些步骤(甚至其中几个步骤),因此我会使用基本功能:

抓取前半部分(基于子字符串"000000"的第三个位置),并将其哈希值与接下来的20个字节进行比较,丢弃所有剩余的内容.

使用: strpos() substr()
或:/$(.*?0{6}.*?0{6}.*?)0{6}/

然后在那之后抓取接下来的19个字节,并将其分成8个(折腾1)和8个.

使用:substr()-(我假设您的意思是17个字节-8 +1 + 8)

$part1 = substr($myStr, $currPos, 8);
$part2 = substr($myStr, $currPos + 9, 8);

I'm writing PHP code to parse a string. It needs to be as fast as possible, so are regular expressions the way to go? I have a hunch that PHP string functions are more expensive, but it's just a guess. What's the truth?

Here's specifically what I need to do with the string:

Grab the first half (based on the third location of a substring "000000") and compare its hash to the next 20 bytes, throwing away anything left.

Parse the 9th byte through the next "000000" as one piece of data. Then grab the next 19 bytes after that, and split that into 8 (toss 1) and 8. Then I do some other stuff that converts those two 8 byte strings into dates.

So that's the kind of thing I need to do.

解决方案

It depends on your case: if you're trying to do something fairly basic (eg: search for a string, replace a substring with something else), then the regular string functions are the way to go. If you want to do something more complicated (eg: search for IP addresses), then the Regex functions are definitely a better choice.

I haven't profiled regexes so I can't say that they'll be faster at runtime, but I can tell you that the extra time spent hacking together the equivalent using the basic functions wouldn't be worth it.


Edit with the new information in the OP:

It sounds as though you actually need to do a number of small string operations here. Since each one individually is quite basic, and I doubt you'd be able to do all those steps (or even a couple of those steps) at one time using a regex, I'd go with the basic functions:

Grab the first half (based on the third location of a substring "000000") and compare its hash to the next 20 bytes, throwing away anything left.

Use: strpos() and substr()
Or : /$(.*?0{6}.*?0{6}.*?)0{6}/

Then grab the next 19 bytes after that, and split that into 8 (toss 1) and 8.

Use: substr() - (I assume you mean 17 bytes here -- 8 + 1 + 8)

$part1 = substr($myStr, $currPos, 8);
$part2 = substr($myStr, $currPos + 9, 8);

这篇关于PHP字符串函数或PHP中的正则表达式哪个更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆