【问题标题】:What do I need for doing this regex or split?做这个正则表达式或拆分我需要什么?
【发布时间】:2015-01-26 13:20:16
【问题描述】:

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

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 行 => [...] 没有拆分(这对我的正则表达式来说是正常的)

【问题讨论】:

  • 2 次完成。先用逗号分割,然后匹配到数组的第三个元素。
  • 这似乎可以在不使用正则表达式的情况下实现。您是否尝试过对您感兴趣的特定字符使用 split 函数来解决此问题?
  • 我得到的这个字符串,来自一个PHP表单,可以完全不同,有3个以上的部分,并且可以尽可能多地重复这个例子。

标签: php regex split


【解决方案1】:

单次通过的一种方法是使用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
)

【讨论】:

  • 这很酷,但太严格了。我的数据因输入数据而异(请参阅编辑)。解决方案必须是全局的。
  • 如果样本输入,我认为您需要为每一行显示预期的匹配项。
【解决方案2】:

它需要是正则表达式吗?有可能通过几个explodes 和foreach 循环来实现这一点:

$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)
 */

【讨论】:

  • 此解决方案仅适用于提供的第一个和第三个字符串
  • 这是我想要的开始,谢谢。但它不适用于我的其他示例数据,必须是更全局的。
【解决方案3】:

在@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);
}

有效!

【讨论】:

  • 1+ 很高兴您找到了解决方案 :)
猜你喜欢
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2012-02-13
  • 1970-01-01
  • 2014-07-13
相关资源
最近更新 更多