Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拆分逗号分隔的字符串(php+;regex)_Php_Regex - Fatal编程技术网

拆分逗号分隔的字符串(php+;regex)

拆分逗号分隔的字符串(php+;regex),php,regex,Php,Regex,我试图从这个格式化代码中提取: name, desc,{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag} 分为三个参数,如 name desc {tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text te

我试图从这个格式化代码中提取:

name, desc,{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}
分为三个参数,如

name
desc
{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}
我已经得到了一些关于:
list($field1,$field2,$field3)=预拆分(“
[,]
”,$param),但我被这个卡住了。
问题在于内部的逗号

有什么帮助吗? 提前感谢。

在PHP中,您可以执行以下操作:

$s = 'name, desc,{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}';

$arr = preg_split('~{tag=.+?{/tag}(*SKIP)(*F)|\h*,\h*~', $s);

print_r($arr);
输出:

Array
(
    [0] => name
    [1] => desc
    [2] => {tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}
)


{tag=.+?{/tag}(*SKIP)(*F)
将匹配起始标记和结束标记之间的所有文本,并将跳过该文本。

为什么不完成解决方法

$param = "name, desc,{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}";

$s = split(',', $param);
$s[2] = implode(',', array_splice($s,2));
var_dump($s);

array(3) {
  [0]=>
  string(4) "name"
  [1]=>
  string(5) " desc"
  [2]=>
  string(120) "{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}"
}

您可以将第三个参数用于
函数(您只需要三段用逗号分隔的字符串,因此只需将其告知
explode()
函数即可):


多亏了大家,现在我有了一个解决方案。这是错误的。用以下方法测试:
$param='{tag=name,100,type}text text text,text text,text text,{/tag},name,desc';
为什么您认为顺序可能会改变?我相信OP只是试图避免拆分标记中包含的字符串。顺序可以是任何东西,因为
标记可能在任何地方。询问OP:)由于“name”和“desc”-它们总是在前面,然后参数:)在这种情况下,您应该使用
explode的第三个参数(
split
自PHP5.3以来已被弃用)为了将项目限制为3个(假设总是有3个项目),无需使用
array\u splice
<?php
$str = 'name, desc,{tag=name,100,type}text text,text text,text text, {/tag}{tag=name,100,type}text text,text text,text text,text text{/tag}';

$arr = array_map(
        function($v) { return trim($v); },
        explode(',', $str, 3) // <-- Actually, this statement is the only thing you need :) 
       );

echo '<pre>';
print_r($arr);
echo '</pre>';