Php 如何将带前导字符和尾随字符的分隔字符串拆分到数组中?
例如,如果我有以下字符串:Php 如何将带前导字符和尾随字符的分隔字符串拆分到数组中?,php,explode,trim,delimited-text,substring,Php,Explode,Trim,Delimited Text,Substring,例如,如果我有以下字符串: $stuff = "[1379082600-1379082720],[1379082480-1379082480],[1379514420-1379515800],"; 我知道可以这样将其拆分为如下数组: $stuff = str_replace(array("[","]"),array("",""),$stuff); $stuff = explode(",",$stuff); 但似乎有更简单的方法,因为字符串几乎已经是数组形式了。有更简单的方法吗?我想这是你能得
$stuff = "[1379082600-1379082720],[1379082480-1379082480],[1379514420-1379515800],";
我知道可以这样将其拆分为如下数组:
$stuff = str_replace(array("[","]"),array("",""),$stuff);
$stuff = explode(",",$stuff);
但似乎有更简单的方法,因为字符串几乎已经是数组形式了。有更简单的方法吗?我想这是你能得到的最好的方法了
$stuff = array_filter(explode(",",str_replace(array("[","]"),"",$stuff)));
print_r($stuff);
[0] => 1379082600-1379082720
[1] => 1379082480-1379082480
[2] => 1379514420-1379515800
我想这和你能得到的一样好
$stuff = array_filter(explode(",",str_replace(array("[","]"),"",$stuff)));
print_r($stuff);
[0] => 1379082600-1379082720
[1] => 1379082480-1379082480
[2] => 1379514420-1379515800
修剪前导和尾随字符,然后吐在
],[
:
$stuff = explode('],[', trim($stuff, '[],');
修剪前导和尾随字符,然后吐在
],[
:
$stuff = explode('],[', trim($stuff, '[],');
因为字符串几乎已经是数组形式
就编程语言而言,字符串和数组是完全不同的东西
有没有更简单的方法
寻找“更简单的方法”是毫无意义的。你现在的方法已经很简单了
因为字符串几乎已经是数组形式
就编程语言而言,字符串和数组是完全不同的东西
有没有更简单的方法
寻找“更简单的方法”是毫无意义的。你现在的方法已经很简单了。你可以通过
预匹配进入[]
。尝试以下方法:
preg_match_all("/\[(.*?)\]/",$stuff, $matches);
$matches的输出[1]
您可以使用preg\u match\u all
进入[]
。请尝试以下操作:
preg_match_all("/\[(.*?)\]/",$stuff, $matches);
$matches的输出[1]
使用基于正则表达式的解决方案将比其他方法慢/效率低。
如果您认为“simpler”是指“更少的函数调用”,那么我建议您使用preg\u split()
或preg\u match\u all()
。不过,我想解释一下preg\u match\u all()
在全局范围中添加一个变量,preg_split不必这样做。此外,preg_match_会生成一个多维数组,您只需要一个一维数组——这是preg_split的另一个优点
这是一组选项。有些是我的,有些是别人发布的。有些有用,有些比别人好,有些不有用。现在是教育时间
$stuff="[1379082600-1379082720],[1379082480-1379082480],[1379514420-1379515800],";
// NOTICE THE TRAILING COMMA ON THE STRING!
// my preg_split() pattern #1 (72 steps):
var_export(preg_split('/[\],[]+/',$stuff,NULL,PREG_SPLIT_NO_EMPTY));
// my preg_split() pattern #2 (72 steps):
var_export(preg_split('/[^\d-]+/',$stuff,NULL,PREG_SPLIT_NO_EMPTY));
// my preg_match_all pattern #1 (16 steps):
var_export(preg_match_all('/[\d-]+/',$stuff,$matches)?$matches[0]:'failed');
// my preg_match_all pattern #2 (16 steps):
var_export(preg_match_all('/[^\],[]+/',$stuff,$matches)?$matches[0]:'failed');
// Bora's preg_match_all pattern (144 steps):
var_export(preg_match_all('/\[(.*?)\]/',$stuff,$matches)?$matches[0]:'failed');
// Alex Howansky's is the cleanest, efficient / correct method
var_export(explode('],[',trim($stuff,'[],')));
// Andy Gee's method (flawed / incorrect -- 4 elements in output)
var_export(explode(",",str_replace(array("[","]"),"",$stuff)));
// OP's method (flawed / incorrect -- 4 elements in output)
$stuff = str_replace(array("[","]"),array("",""),$stuff);
$stuff = explode(",",$stuff);
var_export($stuff);
如果你想看电影的话
如果您想查看我提供的模式中的步骤计数和交换。使用基于正则表达式的解决方案将比其他方法更慢/效率更低。
如果您认为“simpler”是指“更少的函数调用”,那么我建议您使用preg\u split()
或preg\u match\u all()
。不过,我想解释一下preg\u match\u all()
在全局范围中添加一个变量,preg_split不必这样做。此外,preg_match_会生成一个多维数组,您只需要一个一维数组——这是preg_split的另一个优点
这是一组选项。有些是我的,有些是别人发布的。有些有用,有些比别人好,有些不有用。现在是教育时间
$stuff="[1379082600-1379082720],[1379082480-1379082480],[1379514420-1379515800],";
// NOTICE THE TRAILING COMMA ON THE STRING!
// my preg_split() pattern #1 (72 steps):
var_export(preg_split('/[\],[]+/',$stuff,NULL,PREG_SPLIT_NO_EMPTY));
// my preg_split() pattern #2 (72 steps):
var_export(preg_split('/[^\d-]+/',$stuff,NULL,PREG_SPLIT_NO_EMPTY));
// my preg_match_all pattern #1 (16 steps):
var_export(preg_match_all('/[\d-]+/',$stuff,$matches)?$matches[0]:'failed');
// my preg_match_all pattern #2 (16 steps):
var_export(preg_match_all('/[^\],[]+/',$stuff,$matches)?$matches[0]:'failed');
// Bora's preg_match_all pattern (144 steps):
var_export(preg_match_all('/\[(.*?)\]/',$stuff,$matches)?$matches[0]:'failed');
// Alex Howansky's is the cleanest, efficient / correct method
var_export(explode('],[',trim($stuff,'[],')));
// Andy Gee's method (flawed / incorrect -- 4 elements in output)
var_export(explode(",",str_replace(array("[","]"),"",$stuff)));
// OP's method (flawed / incorrect -- 4 elements in output)
$stuff = str_replace(array("[","]"),array("",""),$stuff);
$stuff = explode(",",$stuff);
var_export($stuff);
如果你想看电影的话
如果您想查看我提供的模式中的步骤计数和交换。这是什么[1379082600-1379082720]-您需要将[]的内容视为单个值、减法还是两个子元素?这是什么[1379082600-1379082720]-您需要[]的内容吗被视为单值、减法或两个子元素?为什么有人对此进行否决?这看起来是一个很好的答案。我对这种方法进行否决,因为它不正确/没有提供所需的输出。(它犯的错误与OP的编码尝试相同)为什么有人对此投了反对票?对我来说,这似乎是一个很好的答案。我之所以投反对票,是因为它不正确/没有提供所需的输出。(它犯的错误与OP的编码尝试相同)此解决方案提高了代码的长度、速度和可读性。无论它是否“更容易”完全主观。此解决方案提高了代码的长度、速度和可读性。是否“更容易”完全是主观的。