【问题标题】:How to extract numbers from a string and add to an array?如何从字符串中提取数字并添加到数组中?
【发布时间】:2019-05-26 16:26:49
【问题描述】:

我要转换这个字符串:

<5> 20825.24 </ 5> <7> 0.0 </ 7> <8> 0.0 </ 8>

到具有键值的数组

非常感谢您的帮助

我尝试了不同的 xmlParse 策略,但它并不完全是 xml。

$xml = new \SimpleXMLElement("<5>20825.24</5><7>0.0</ 7><8>0.0</8>");

响应:字符串无法解析为 XML

我期望这样的数组:

[5=>20825.24,7=>0.0,8=>0.0]

【问题讨论】:

  • XML 标签名称不能以数字开头,因此这意味着除非您处理数据 - 它不是有效的 XML(也没有根节点 - 但这很容易修复)

标签: php regex split preg-match-all regex-lookarounds


【解决方案1】:

只要您的输出在 XML 标记名称的开头有数字,它就不是有效的 XML。您可能不得不求助于使用正则表达式来完成这项工作。

这使用&lt;(\d*)&gt;(.*?)&lt;/~ 查找&lt; 后跟数字,然后是&gt; 并捕获直到下一个&lt;/ 的所有内容。然后它将捕获组 1(标签名称)和 2(值)中的值组合在一起......

$data = "<5>20825.24</5><7>0.0</ 7><8>0.0</8>";

preg_match_all("~<(\d*)>(.*?)</~", $data, $matches);
$output = array_combine($matches[1], $matches[2]);
print_r($output);

给...

Array
(
    [5] => 20825.24
    [7] => 0.0
    [8] => 0.0
)

【讨论】:

  • 这很简洁,比我的答案更简洁。
【解决方案2】:
<?php

$subject = '<5> 20825.24 <7> 0.0 <8> 0.0';
$pattern = '/\<(\d)\>\s+(\d+\.\d+)/u';
$result = preg_match_all($pattern,$subject,$output);

$numbers = $output[1];
$output = $output[2];
$outTotal = [];

foreach ($numbers as $key => $number) {
    $outTotal[$number] = $output[$key];
}
var_dump($outTotal);

给予:

array(3) {
  [5]=>
  string(8) "20825.24"
  [7]=>
  string(3) "0.0"
  [8]=>
  string(3) "0.0"
}

【讨论】:

    【解决方案3】:

    在这里,我们也可以使用正则表达式,收集我们想要的数字,然后将其推送到一个新数组中:

    (?:<)(\d?)(?:>)(?:.+?)([0-9.]+)?
    

    此表达式也可能适用于nested elements。

    测试

    $re = '/(?:<)(\d?)(?:>)(?:.+?)([0-9.]+)?/m';
    $str = '<5> 20825.24 </ 5> <7> 0.0 </ 7> <8> 0.0 </ 8>';
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    $arr = array();
    foreach ($matches as $key => $value) {
        $arr[$value[1]] = $value[2];
    }
    
    var_dump($arr);
    

    输出

    array(3) {
      [5]=>
      string(8) "20825.24"
      [7]=>
      string(3) "0.0"
      [8]=>
      string(3) "0.0"
    }
    

    DEMO

    正则表达式电路

    jex.im 可视化正则表达式:

    【讨论】:

      【解决方案4】:

      如果您想将开始标签中的数字与结束标签中的数字匹配,您可以使用正则表达式,将backreference 用于第一个捕获组:

      <(\d+)>\h*\d+(?:\.\d+)?\h*</\h*\1>
      

      说明

      • &lt;匹配&gt;char
      • (\d+)&gt; 在组 1 中捕获匹配 1+ 个数字并匹配 &gt;
      • \h* 匹配 0+ 个水平空白字符
      • \d+(?:\.\d+)? 匹配 1+ 位数字,后跟可选部分以匹配点和 1+ 位数字
      • \h* 匹配 0+ 个水平空白字符
      • &lt;匹配&lt;
      • /\h* 匹配 \ 后跟 0+ 个水平空白字符
      • \1&gt; 使用 \1 将反向引用匹配到组 1 并匹配 &gt;

      Regex demo | Php demo

      例如:

      $re = '~<(\d+)>\h*(\d+(?:\.\d+)?)\h*</\h*\1>~m';
      $str = '<5> 20825.24 </ 5> <7> 0.0 </ 7> <8> 0.0 </ 8>';
      preg_match_all($re, $str, $matches);
      print_r(array_combine($matches[1], $matches[2]));
      

      结果

      Array
      (
          [5] => 20825.24
          [7] => 0.0
          [8] => 0.0
      )
      

      请注意,现在数组键是 5、7 和 8。请记住,如果您可能使用不同的数据,则数组不能有重复的键。

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 2016-01-04
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-09
        相关资源
        最近更新 更多