PHP preg_split 仅在双引号内
PHP preg_split only inside double quotes
如何使用 preg_split 函数
仅获取双引号内的值
来自这个示例字符串
$String = '["number","1","2470A K18-901","PEDAL ASSY, GEAR CHANGE","1","PCS","56500.00","0","56500","action"]'
变成这样:
Array
(
[0] => number
[1] => 1
[2] => 2470A K18-901
[3] => PEDAL ASSY, GEAR CHANGE
[4] => 1
[5] => PCS
[6] => 56500.00
[7] => 0
[8] => 56500
[9] => action
)
或者用另一个函数来实现
如果您希望拆分的值实际上是字符串类型(如您的问题所示),您可以使用 preg_match_all
代替(preg_match_all
基本上是 一个 行来匹配所有必需的值;使用 preg_split
你必须 trim 值周围的括号)。
<?php
$String = '["number","1","2470A K18-901","PEDAL ASSY, GEAR CHANGE","1","PCS","56500.00","0","56500","action"]';
preg_match_all('/(?<=,"|\[")[^"]+?(?=")/', $String, $Match);
$Result = $Match[0];
var_dump($Result);
PS:此解决方案假定数组 中的值没有任何转义引号 .
如果要将字符串转换为数组,请使用 json_decode:
<?php
$jsonobj = '["number","1","2470A K18-901","PEDAL ASSY, GEAR
CHANGE","1","PCS","56500.00","0","56500","action"]';
print_r(json_decode($jsonobj));
?>
输出:
Array ( [0] => number [1] => 1 [2] => 2470A K18-901 [3] => PEDAL ASSY, GEAR CHANGE [4] => 1 [5] => PCS [6] => 56500.00 [7] => 0 [8] => 56500 [9] => 动作)
这是一个 JSON 字符串,您可以使用 json_decode()
函数将其转换为数组。 https://www.php.net/manual/de/function.json-decode.php
<?php
$String = '["number","1","2470A K18-901","PEDAL ASSY, GEAR CHANGE","1","PCS","56500.00","0","56500","action"]';
print_r(json_decode($String));
如何使用 preg_split 函数
仅获取双引号内的值来自这个示例字符串
$String = '["number","1","2470A K18-901","PEDAL ASSY, GEAR CHANGE","1","PCS","56500.00","0","56500","action"]'
变成这样:
Array
(
[0] => number
[1] => 1
[2] => 2470A K18-901
[3] => PEDAL ASSY, GEAR CHANGE
[4] => 1
[5] => PCS
[6] => 56500.00
[7] => 0
[8] => 56500
[9] => action
)
或者用另一个函数来实现
如果您希望拆分的值实际上是字符串类型(如您的问题所示),您可以使用 preg_match_all
代替(preg_match_all
基本上是 一个 行来匹配所有必需的值;使用 preg_split
你必须 trim 值周围的括号)。
<?php
$String = '["number","1","2470A K18-901","PEDAL ASSY, GEAR CHANGE","1","PCS","56500.00","0","56500","action"]';
preg_match_all('/(?<=,"|\[")[^"]+?(?=")/', $String, $Match);
$Result = $Match[0];
var_dump($Result);
PS:此解决方案假定数组 中的值没有任何转义引号 .
如果要将字符串转换为数组,请使用 json_decode:
<?php
$jsonobj = '["number","1","2470A K18-901","PEDAL ASSY, GEAR
CHANGE","1","PCS","56500.00","0","56500","action"]';
print_r(json_decode($jsonobj));
?>
输出:
Array ( [0] => number [1] => 1 [2] => 2470A K18-901 [3] => PEDAL ASSY, GEAR CHANGE [4] => 1 [5] => PCS [6] => 56500.00 [7] => 0 [8] => 56500 [9] => 动作)
这是一个 JSON 字符串,您可以使用 json_decode()
函数将其转换为数组。 https://www.php.net/manual/de/function.json-decode.php
<?php
$String = '["number","1","2470A K18-901","PEDAL ASSY, GEAR CHANGE","1","PCS","56500.00","0","56500","action"]';
print_r(json_decode($String));