如何在PHP中按空格,数字和引号拆分字符串?

时间:2022-08-22 12:55:13

I am parsing a string in PHP that has the following pattern

我在PHP中解析一个具有以下模式的字符串

VARIABLE Key1 Value1 Key2 Value2 Key3 Value3 ...

similar to:

JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'

OR

SOMEVARIABLE 1 "Value1" 2 'Value2'

This line starts with an unquoted string and can have single or double quoted strings and/or numbers. It can have one to multiple key value pairs.

该行以不带引号的字符串开头,可以包含单引号或双引号字符串和/或数字。它可以有一对多的键值对。

I need to split the string in 2 ways:

我需要以两种方式拆分字符串:

The first to get the unquoted string that is not numeric.

第一个得到不带数字的不带引号的字符串。

The second to extract the numeric value and/or quoted strings - can be single or dobule

第二个提取数值和/或引用的字符串 - 可以是单个或多个

Thus I need

因此我需要

  1. JOBGRADE
  2. P:Parttime Employee
  3. C:Customer Support

OR

  1. SOMEVARIABLE
  2. 1:Value1
  3. 2:Value2

My Thoughts:

I thought about splitting the string and iterating through it to test:

我想过拆分字符串并迭代它来测试:

for 1: If value is not numeric and not quoted it is the variable name

for 1:如果value不是数字而没有引用,则它是变量名

for 2+: Not sure the easy way to do this because I must detect the difference between the keys and values:

for 2+:不确定这样做的简单方法,因为我必须检测键和值之间的差异:

Question:

How can I distinguish between the key/value?

如何区分键/值?

2 个解决方案

#1


11  

Treat it as CSV, and iterate over it to divide it up. The variable is [0], keys are odd starting from [1], values even from [2].

将其视为CSV,并对其进行迭代以将其分割。变量为[0],键从[1]开始为奇数,值为[2]。

var_dump(str_getcsv("JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'",
  ' ', "'"));
array(5) {
  [0]=>
  string(8) "JOBGRADE"
  [1]=>
  string(1) "P"
  [2]=>
  string(17) "Parttime Employee"
  [3]=>
  string(1) "C"
  [4]=>
  string(16) "Customer Support"
}

#2


1  

first use explode() on the variable string to get all parts seperated by one space: http://php.net/manual/en/function.explode.php

首先在变量字符串上使用explode()来将所有部分分隔为一个空格:http://php.net/manual/en/function.explode.php

$variables = explode("JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'", ' ');

//i wouldn't use the first item so remove it, keep as as title for later

//我不会使用第一个项目,所以删除它,保留标题以供日后使用

$var_name = array_shift($variables);

//secondly, loop over items (step 2) and add to resulting array the key & value

//其次,循环遍历项目(步骤2)并将结果和值添加到结果数组中

$result = array();

for ($i = 0; $i < count($variables); $i = $i +2) {
  $result[$variables[$i]] = $variables[$i + 1];
}

print_r($result);

#1


11  

Treat it as CSV, and iterate over it to divide it up. The variable is [0], keys are odd starting from [1], values even from [2].

将其视为CSV,并对其进行迭代以将其分割。变量为[0],键从[1]开始为奇数,值为[2]。

var_dump(str_getcsv("JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'",
  ' ', "'"));
array(5) {
  [0]=>
  string(8) "JOBGRADE"
  [1]=>
  string(1) "P"
  [2]=>
  string(17) "Parttime Employee"
  [3]=>
  string(1) "C"
  [4]=>
  string(16) "Customer Support"
}

#2


1  

first use explode() on the variable string to get all parts seperated by one space: http://php.net/manual/en/function.explode.php

首先在变量字符串上使用explode()来将所有部分分隔为一个空格:http://php.net/manual/en/function.explode.php

$variables = explode("JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'", ' ');

//i wouldn't use the first item so remove it, keep as as title for later

//我不会使用第一个项目,所以删除它,保留标题以供日后使用

$var_name = array_shift($variables);

//secondly, loop over items (step 2) and add to resulting array the key & value

//其次,循环遍历项目(步骤2)并将结果和值添加到结果数组中

$result = array();

for ($i = 0; $i < count($variables); $i = $i +2) {
  $result[$variables[$i]] = $variables[$i + 1];
}

print_r($result);