php - strpos是在大量文本中搜索字符串的最快方法吗? [英] php - Is strpos the fastest way to search for a string in a large body of text?

查看:787
本文介绍了php - strpos是在大量文本中搜索字符串的最快方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (strpos(htmlentities($storage->getMessage($i)),'chocolate')) 

我使用gmail oauth访问来查找电子邮件地址中的特定文本字符串。有没有比在上面的代码中使用strpos更快,更有效地找到文本实例的方法?我应该使用散列技术吗?

Hi, I'm using gmail oauth access to find specific text strings in email addresses. Is there a way to find text instances quicker and more efficiently than using strpos in the above code? Should I be using a hash technique?

推荐答案

根据PHP手册,是 - strpos()是确定一个字符串是否包含另一个字符串的最快捷方式。

According to the PHP manual, yes- strpos() is the quickest way to determine if one string contains another.


注意:

Note:

如果您只想确定干草堆内是否存在特定的针,则
将使用速度更快,占用内存更少的函数strpos()。

If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

这是在任何php.net文章中一次又一次引用的关于其他字符串比较器的文章(我从 strstr()

This is quoted time and again in any php.net article about other string comparators (I pulled this one from strstr())

尽管您的陈述应该有两个变化。

Although there are two changes that should be made to your statement.

if (strpos($storage->getMessage($i),'chocolate') !== FALSE)

这是因为 if(0)的计算结果为false(因此不会运行),但是 strpos()可返回0。另外,除去 htmlentities()将使您的代码运行速度更快。所有 htmlentities()都会将某些字符替换为适当的HTML等效字符。例如,它将& 替换为& amp;

This is because if(0) evaluates to false (and therefore doesn't run), however strpos() can return 0 if the needle is at the very beginning (position 0) of the haystack. Also, removing htmlentities() will make your code run a lot faster. All that htmlentities() does is replace certain characters with their appropriate HTML equivalent. For instance, it replaces every & with &

正如您可以想象的那样,单独检查字符串中的每个字符并替换其中的很多字符都需要额外的内存和处理器能力。不仅如此,如果你只是做一个文本比较就没有必要了。例如,比较以下语句:

As you can imagine, checking every character in a string individually and replacing many of them takes extra memory and processor power. Not only that, but it's unnecessary if you plan on just doing a text comparison. For instance, compare the following statements:

strpos('Billy & Sally', '&'); // 6
strpos('Billy & Sally', '&'); // 6
strpos('Billy & Sally', 'S'); // 8
strpos('Billy & Sally', 'S') // 12


$ b $或者,在最糟糕的情况下,你甚至可能会导致某些事情真的被评估为假。

Or, in the worst case, you may even cause something true to evaluate to false.

strpos('<img src...', '<'); // 0
strpos('&lt;img src...','<'); // FALSE

为了避免这种情况,您最终会使用更多的HTML实体。 p>

In order to circumvent this you'd end up using even more HTML entities.

strpos('&lt;img src...', '&lt;'); // 0

但是,正如您可以想象的那样,这不仅会让代码烦躁,而且会变得多余。你最好不要完全排除HTML实体。通常只有在输出文本时才使用HTML实体。没有比较。

But this, as you can imagine, is not only annoying to code but gets redundant. You're better off excluding HTML entities entirely. Usually HTML entities is only used when you're outputting text. Not comparing.

这篇关于php - strpos是在大量文本中搜索字符串的最快方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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