2
A
回答
15
如果只是这一点,那么你可以使用纯文本搜索:
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
从来就不是一个好的选择。
这里的长版本:
- 两个不同的 “干草堆”:的lipsum的lipsum
- 83个字符
- 10000个字符。
- 5种不同的搜索方法:
- strpos:
return strpos($haystack, $needle) === 0
- preg_match
return preg_match("/^$needle/", $haystack) === 1
- substr
return substr($haystack, 0, strlen($needle)) === $needle
- strncmp
return strncmp($needle, $haystack, strlen($needle)) === 0
- 手册循环:
- strpos:
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个字符针头除了最后一个字符外都匹配。
- 100000迭代,每个方法每针每草堆
有意思的观点:
- 所有的最快的个人测试是
strpos
上对短长,完全不匹配针草垛。- 事实上,每出方法运行14次测试,
strpos
记录的顶部11倍。
- 事实上,每出方法运行14次测试,
- 最慢的单独测试是长针上的手动方法,无论干草堆的大小如何。这四项测试比几乎所有其他测试慢10-20倍。
- 虽然
strpos
有最好的表现,但它被长长的干草堆上长长的不匹配的针头压下。他们比大多数测试慢5-10倍。 strncmp
很快和最一致。preg_match
始终比其他功能
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 | 0.1596 | 0.1633 | 0.1537 | 0.1560 | 0.1571 | 0.1589 | 0.1681 |
| 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 | 0.1914 | 0.1835 | 0.2014 | 0.1851 | 0.1854 | 0.1989 |
| strpos | 0.1605 | 2.1877 | 2.3737 | 0.5933 | 0.1575 | 0.1597 | 0.1667 |
| substr | 0.2073 | 0.2085 | 0.2017 | 0.2152 | 0.2036 | 0.2090 | 0.2183 |
-----------------------------------------------------------------------------
0
如果你有read the first example in the documentation你会看到答案。
if (preg_match('/^for the/i', $sentence))
{
// a match was found
}
0
的正则表达式是/^for the/i
1
如何
if(preg_match("/^for the/", $yourString))
{
return true;
}
else
{
return false;
}
注^
代码,开始匹配大约慢2倍一个字符串。
3
你想用^
来表示一个字符串的开头:
$string_one = "For the love of Mike";
$string_two = "for the amazing reason.";
$match = preg_match("/^for the/i", $string_one); // Outputs 1
$match = preg_match("/^for the/i", $string_two); // Outputs 1
的/i
是使得搜索不区分大小写的部分。
相关问题
- 1. preg_match为“^/page /(\ d +)$”
- 2. 为的preg_match
- 3. Youtube preg_match为endscreen
- 4. preg_match中的preg_match
- 5. 多的preg_match以多行
- 6. 的preg_match以相当于.NET
- 7. 有人可以帮我preg_match?
- 8. 是否可以嵌套preg_match?
- 9. 意外的PREG_MATCH行为
- 10. PHP - 为的preg_match(串),(串),
- 11. 的preg_match为MgmtEth0 \/RSP1 \/CPU0 \/0/
- 12. 的preg_match()为我的网址
- 13. PHP - 将ereg转换为preg_match
- 14. 的preg_match意外行为
- 15. 从preg_match preg_match使用php
- 16. preg_match防止以PHP格式注入
- 17. preg_match所有单词都以@开始?
- 18. preg_match array
- 19. PHP - preg_match?
- 20. preg_match foreach
- 21. 的preg_match
- 22. preg_match help
- 23. PHP - preg_match()
- 24. Curl preg_match
- 25. 小preg_match
- 26. 替代,如果(的preg_match()和的preg_match())
- 27. 为什么不显示preg_match的结果?
- 28. preg_match文件列表的扩展名为
- 29. 如何将eregi转换为preg_match
- 30. 将preg_match模式转换为javascript模式
+1,正则表达式比一般strpos更多资源密集的()和相关的。 – 2010-01-12 14:03:17
这是我的第一直觉为好,但它的大串非常低效。即使$文本不是以“for”开始,它也会继续搜索字符串的其余部分,这可能是数千字节。 – 2010-01-12 14:03:55
@Jordan,是不是真正的正则表达式? – 2010-01-12 14:05:25