php - preg_match 用于以 'for the' 开头的句子

标签 php preg-match

非常简单,但我无法使用准确的语法。

我只想要一个真假检查,看看字符串是否带有“for the”(不区分大小写)。

最佳答案

如果只是这样,那么您可以使用纯文本搜索:

if (stripos("for the", $text) === 0) { // case-insensitive here
    // string starts with "for the"
}

或者,

if (substr($text, 0, 7) == "for the")

下面的评论让我想知道哪个实际上更快,所以我写了一些基准测试。

这是 TLDR 版本:

    如果您不使用大字符串,
  • strpos 会非常快。
  • strncmp 可靠且快速。
  • preg_match 从来都不是一个好的选择。

这是长版:

  • 两个不同的“干草堆”:
    1. 10000 个字符 lipsum
    2. 83字唇膏。
  • 5 种不同的搜索方法:
    1. strpos :
      返回 strpos($haystack, $needle) === 0
    2. preg_match
      返回 preg_match("/^$needle/", $haystack) === 1
    3. substr
      返回 substr($haystack, 0, strlen($needle)) === $needle
    4. strncmp
      返回 strncmp($needle, $haystack, strlen($needle)) === 0
    5. 手动循环:
for ($i = 0, $l = strlen($needle); $i < $l; ++$i) {
    if ($needle{$i} !== $haystack{$i}) return false;
}
return true;
  • 7 种不同的“针”
    • 3 个匹配(长度:83、5 和 1 个字符)
    • 4 个不匹配(长度:83、82、5 和 1 个字符)。 82 个字符的针根本不匹配,而 83 个字符的针除了最后一个字符之外的所有字符都匹配。
  • 100,000 次迭代,每个方法每个针每个大海捞针

兴趣点:

  • 最快的个人测试是 strpos 在长的、完全不匹配的针上对抗短的大海捞针。
    • 事实上,在每种方法运行的 14 次测试中,strpos 记录了前 11 次。
  • 最慢的个人测试是长针上的手动方法,无论干草堆大小如何。这四项测试比几乎所有其他测试都慢 10-20 倍。
  • 虽然 strpos 的性能最好,但它被大海捞针上长长的不匹配的针拖累了。它们比大多数测试慢 5-10 倍。
  • strncmp 速度并且最稳定。
  • preg_match 始终比其他函数慢 2 倍
Haystack: 83 characters
              ______________________________________________________________
 ____________|__________ non-matching ___________|_______  matching ________|
| function   |   1    |   5    |   82   |   83   |   1    |   5    |   83   |
|------------+--------+--------+--------+--------+--------+--------+--------|
| manual     | 0.2291 | 0.2222 | 0.2266 | 4.1523 | 0.2337 | 0.4263 | 4.1972 |
| preg_match | 0.3622 | 0.3792 | 0.4098 | 0.4656 | 0.3642 | 0.3694 | 0.4658 |
| strncmp    | 0.1860 | 0.1918 | 0.1881 | 0.1981 | 0.1841 | 0.1857 | 0.1980 |
| strpos     <strong>| 0.1596 | 0.1633 | 0.1537 | 0.1560 | 0.1571 | 0.1589 | 0.1681 |</strong>
| substr     | 0.2052 | 0.2066 | 0.2009 | 0.2166 | 0.2061 | 0.2017 | 0.2236 |
-----------------------------------------------------------------------------

Haystack: 10000 characters
              ______________________________________________________________ 
 ____________|__________ non-matching ___________|_______  matching ________|
| function   |   1    |   5    |   82   |   83   |   1    |   5    |   83   |
|------------+--------+--------+--------+--------+--------+--------+--------|
| manual     | 0.2275 | 0.2249 | 0.2278 | 4.1507 | 0.2315 | 0.4233 | 4.1834 |
| preg_match | 0.3597 | 0.3628 | 0.4147 | 0.4654 | 0.3662 | 0.3679 | 0.4684 |
| strncmp    | 0.1886 | <strong>0.1914</strong> | <strong>0.1835</strong> | <strong>0.2014</strong> | 0.1851 | 0.1854 | 0.1989 |
| strpos     | <strong>0.1605</strong> | 2.1877 | 2.3737 | 0.5933 | <strong>0.1575</strong> | <strong>0.1597</strong> | <strong>0.1667</strong> |
| substr     | 0.2073 | 0.2085 | 0.2017 | 0.2152 | 0.2036 | 0.2090 | 0.2183 |
-----------------------------------------------------------------------------

关于php - preg_match 用于以 'for the' 开头的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2049373/

相关文章:

php - mysql表导入不起作用?

php - 获取百分号前的数字

php - 在此服务器上找不到请求的 URL/ProjectName/users。拉维

javascript - 使用ajax请求删除文件

php - 管理大量文件的技巧?

php - xml 输出节点的 preg 匹配

javascript - 带 'www' 或不带 'www' 的域的正则表达式

Php Regex查找字符串是否为mysql select语句

php - 如何在 else 函数中使用 if 函数

PHP复选框问题