php split() 和explode() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3640990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between split() and explode()?
提问by Theodore R. Smith
The PHP manual for split()
says
PHP手册split()
说
This function has been DEPRECATEDas of PHP 5.3.0. Relying on this feature is highly discouraged...Use
explode()
instead.
自 PHP 5.3.0 起,此函数已被弃用。非常不鼓励依赖此功能...
explode()
改用。
But I can't find a difference between split()
and explode()
. join()
hasn't been deprecated, so what gives?
但我找不到split()
和之间的区别explode()
。join()
还没有被弃用,那么有什么好处呢?
回答by BoltClock
It's been deprecated because
它已被弃用,因为
explode()
is substantially faster because it doesn't split based on a regular expression, so the string doesn't have to be analyzed by the regex parserpreg_split()
is faster and uses PCRE regular expressions for regex splits
explode()
速度要快得多,因为它不基于正则表达式进行拆分,因此字符串不必由正则表达式解析器进行分析preg_split()
更快,并使用 PCRE 正则表达式进行正则表达式拆分
join()
and implode()
are aliases of each other and therefore don't have any differences.
join()
和implode()
是彼此的别名,因此没有任何区别。
回答by Matthew Flaschen
split
uses regex, while explode
uses a fixed string. If you do need regex, use preg_split
, which uses PCRE (the regex package now preferred across the PHP standard library).
split
使用正则表达式,而explode
使用固定字符串。如果您确实需要正则表达式,请使用preg_split
,它使用 PCRE(现在 PHP 标准库中首选的正则表达式包)。
回答by Sampath
Both are used to split a string into an array, but the difference is that split()
uses pattern for splitting whereas explode()
uses string. explode()
is faster than split()
because it doesn't match string based on regular expression.
两者都用于将字符串拆分为数组,但不同之处在于split()
使用模式进行拆分,而explode()
使用字符串。explode()
比split()
因为它不匹配基于正则表达式的字符串快。
回答by Russell Dias
In split()
you can use regular expressions to split a string. Whereas explode()
splits a string with a string.
preg_splitis a much faster alternative, should you need regular expressions.
在split()
你可以使用正则表达式来拆分字符串。而explode()
用字符串拆分字符串。
如果您需要正则表达式,preg_split是一个更快的替代方案。
回答by Russell Dias
Both the functions are used to Split a string.
这两个函数都用于拆分字符串。
However, Split is used to split a string using a regular expression.
但是,Split 用于使用正则表达式拆分字符串。
On the other hand, Explode is used to split a string using another string.
另一方面,Explode 用于使用另一个字符串拆分字符串。
E.gexplode (" this", "this is a string"); will return “Is a string”
例如explode (" this", "this is a string"); 将返回“是一个字符串”
E.gSplit (" + ", "This+ is a string");
例如Split (" + ", "This+ is a string");
回答by rocoder
The split()
function splits the string into an array using a regular expression and returns an array.
该split()
函数使用正则表达式将字符串拆分为一个数组并返回一个数组。
The explode()
function splits the string by string.
该explode()
函数按字符串拆分字符串。
E.g:
例如:
<?php
$split_array=split(':','I:P:S');
$explode_array=explode('and','I and P and S');
print_r($split_array);
print_r($explode_array);
?>
The result will be
结果将是
Array ( [0] => I [1] => P [2] => S )
Array ( [0] => I [1] => P [2] => S )
Note:The function split() was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0
注意:函数 split() 在 PHP 5.3.0 中被弃用,在 PHP 7.0.0 中被移除