This may sound like a stupid question, but: which is faster when using it to extract keywords in a search query in php:
这可能听起来像一个愚蠢的问题,但是:当使用它来提取php中的搜索查询中的关键字时,它会更快:
$keyword = preg_split('/[\s]+/', $_GET['search']);
or
$keyword = explode(' ', $_GET['search']);
3 个解决方案
#1
19
Explode is faster, per PHP.net
根据PHP.net,爆炸速度更快
Tip If you don't need the power of regular expressions, you can choose faster (albeit simpler) alternatives like explode() or str_split().
提示如果您不需要正则表达式的强大功能,您可以选择更快(尽管更简单)的替代方法,如explode()或str_split()。
#2
11
In a simple usage explode()
is than faster, see: micro-optimization.com/explode-vs-preg_split (link from web.archive.org)
在一个简单的用法中,explode()比更快,请参阅:micro-optimization.com/explode-vs-preg_split(来自web.archive.org的链接)
But preg_split
has the advantage of supporting tabs (\t
) and spaces with \s
.
但preg_split的优点是支持标签(\ t)和\ s空格。
the \s
metacharacter is used to find a whitespace character.
\ s元字符用于查找空白字符。
A whitespace character can be (http://php.net/manual/en/regexp.reference.escape.php):
一个空格字符可以是(http://php.net/manual/en/regexp.reference.escape.php):
- space character (32 =
0x20
) - tab character (9 =
0x09
) - carriage return character (13 =
0x0D
) - new line character (10 =
0x0A
) - form feed character (12 =
0x0C
)
空格字符(32 = 0x20)
制表符(9 = 0x09)
回车符(13 = 0x0D)
换行符(10 = 0x0A)
换页字符(12 = 0x0C)
In this case you should see the cost and benefit.
在这种情况下,您应该看到成本和收益。
A tip, use array_filter
for "delete" empty items in array:
提示,使用array_filter“删除”数组中的空项:
Example:
$keyword = explore(' ', $_GET['search']); //or preg_split
print_r($keyword);
$keyword = array_filter($arr, 'empty');
print_r($keyword);
Note: RegExp Perfomance
注意:RegExp Perfomance
#3
6
General rule: if you can do something without regular expressions, do it without them!
一般规则:如果你可以在没有正则表达式的情况下做某事,那就不用它们吧!
if you want to split string by spaces, explode is way faster.
如果你想用空格分割字符串,爆炸会更快。
#1
19
Explode is faster, per PHP.net
根据PHP.net,爆炸速度更快
Tip If you don't need the power of regular expressions, you can choose faster (albeit simpler) alternatives like explode() or str_split().
提示如果您不需要正则表达式的强大功能,您可以选择更快(尽管更简单)的替代方法,如explode()或str_split()。
#2
11
In a simple usage explode()
is than faster, see: micro-optimization.com/explode-vs-preg_split (link from web.archive.org)
在一个简单的用法中,explode()比更快,请参阅:micro-optimization.com/explode-vs-preg_split(来自web.archive.org的链接)
But preg_split
has the advantage of supporting tabs (\t
) and spaces with \s
.
但preg_split的优点是支持标签(\ t)和\ s空格。
the \s
metacharacter is used to find a whitespace character.
\ s元字符用于查找空白字符。
A whitespace character can be (http://php.net/manual/en/regexp.reference.escape.php):
一个空格字符可以是(http://php.net/manual/en/regexp.reference.escape.php):
- space character (32 =
0x20
) - tab character (9 =
0x09
) - carriage return character (13 =
0x0D
) - new line character (10 =
0x0A
) - form feed character (12 =
0x0C
)
空格字符(32 = 0x20)
制表符(9 = 0x09)
回车符(13 = 0x0D)
换行符(10 = 0x0A)
换页字符(12 = 0x0C)
In this case you should see the cost and benefit.
在这种情况下,您应该看到成本和收益。
A tip, use array_filter
for "delete" empty items in array:
提示,使用array_filter“删除”数组中的空项:
Example:
$keyword = explore(' ', $_GET['search']); //or preg_split
print_r($keyword);
$keyword = array_filter($arr, 'empty');
print_r($keyword);
Note: RegExp Perfomance
注意:RegExp Perfomance
#3
6
General rule: if you can do something without regular expressions, do it without them!
一般规则:如果你可以在没有正则表达式的情况下做某事,那就不用它们吧!
if you want to split string by spaces, explode is way faster.
如果你想用空格分割字符串,爆炸会更快。