执行此正则表达式或拆分需要什么?

What do I need for doing this regex or split?

我有如下字符串需要拆分:

Other,CODSITE,Items::getCodCdeCli+Address::getNameAddress
Other,CODSITE,Items::getCodCdeCli
Items::getCode+Address::getName,CODSITE+Items::getSample,Items::getItemID
Other, CODSITE, CODSITE2

进入:

array(
    array(
    0 => 'Other',
    1 => 'CODSITE',
    2 => array(
        'Items' => 'getCodCdeCli',
        'Address' => 'getNameAddress'
       )
    ),
//...
) 

每个逗号都包含新信息,如果我们有一个“+”,我们需要附加两个数据。如果我们有'::',我们需要得到第一部分作为结果信息的关键。

为了开始这个解决方案,我尝试用逗号分隔它:

$re = "/([^,]+)/"; 
$str = "Other,CODSITE,Items::getCodCdeCli+Address::getNameAddress"; 
 
preg_match_all($re, $str, $matches);

现在用这个正则表达式我有这个:

array (size=2)
  0 => 
    array (size=3)
      0 => string 'Other' (length=5)
      1 => string 'CODSITE' (length=7)
      2 => string 'Items::getCodCdeCli+Address::getNameAddress' (length=43)
  1 => 
    array (size=3)
      0 => string 'Other' (length=5)
      1 => string 'CODSITE' (length=7)
      2 => string 'Items::getCodCdeCli+Address::getNameAddress' (length=43)

这是错误的。我有两次相同的结果..并且第 2 行 => [...] 没有拆分(这对于我的正则表达式来说是正常的)

单程执行此操作的一种方法是使用 array_combine function,如下所示:

$str = 'Other,CODSITE,Items::getCodCdeCli+Address::getNameAddress';
if ( preg_match_all('~(?|[,+]([^,+]+)::([^,+]+)|([^,]+))~', $str, $m) )
   print_r( array_combine ( $m[1], $m[2] ) );

输出:

Array
(
    [Other] =>
    [CODSITE] =>
    [Items] => getCodCdeCli
    [Address] => getNameAddress
)

是否需要正则表达式?可以通过几个 explodeforeach 循环来实现:

$str = 'Other,CODSITE,Items::getCodCdeCli+Address::getNameAddress';

//new entry per comma (,)
$results = explode(',',$str);

//check each entry for array information
foreach($results as &$result) {
    if(strpos($result,'+') !== FALSE) {

        //explode array information
        $bits1 = explode('+',$result);
        $result = array();
        foreach($bits1 as &$subresult) {

            //format array information into key => value pairs
            if(strpos($subresult,'::') !== FALSE) {
                $bits = explode('::',$subresult);
                $result[$bits[0]] = $bits[1];
            }
        }
    }
}
var_dump($results);

/**
 * array (size=3)
 *   0 => string 'Other' (length=5)
 *   1 => string 'CODSITE' (length=7)
 *   2 => array (size=2)
 *       'Items' => string 'getCodCdeCli' (length=12)
 *       'Address' => string 'getNameAddress' (length=14)
 */

借助@Richard Parnaby-King 的回答。这是解决方案,实际上不需要正则表达式,即使我确定我们可以使用它来获得相同的结果。

$lines = array(
    0 => 'Other,CODSITE,Items::getCodCdeCli+Address::getNameAddress',
    2 => 'Other,CODSITE,Items::getCodCdeCli',
    3 => 'Items::getCode+Address::getName,CODSITE+Items::getSample,Items::getItemID',
    4 => 'Other, CODSITE, CODSITE2',
);

foreach ($lines as $input) {
    $informations = explode(',', $input);
    $result = array();
    foreach ($informations as $information) {
        if(strpos($information, '+') !== FALSE) {
            $classes = explode('+',$information);
            $temp = array();
            foreach($classes as $subresult) {
                if(strpos($subresult,'::') !== FALSE) {
                    $classAndMethod = explode('::',$subresult);
                    $temp[$classAndMethod[0]] = $classAndMethod[1];
                } else {
                    $temp[] = trim($subresult);
                }
            }
            $result[] = $temp;
        } elseif (strpos($information, '::') !== FALSE) {
            $classAndMethod = explode('::',$information);
            $result[][$classAndMethod[0]] = $classAndMethod[1];
        } else {
            $result[] = trim($information);
        }
    }
    var_dump($result);
}

有效!