【问题标题】:How to get data between tags in array如何在数组中的标签之间获取数据
【发布时间】:2014-07-28 11:07:28
【问题描述】:

标签之间有数据

<ml><locale name="en-US">125LKO.50C-SL137</locale>
     <locale name="es-ES"></locale>
 </ml> 

同样,我想以这种格式将数据放入数组中

 arr['en']= "125LKO.50C-SL137"
 arr['es']= "-";

 arr['0']= "125LKO.50C-SL137"
 arr['1']= "-";

我正在使用这个功能

function get_string_between($string){
 $string = preg_replace('/<[^>]*>/', "~", $string);
 $words = explode('~', $string);
 return array_values(array_filter($words));
}

如何在php中将数据放入数组中,请帮助我。

【问题讨论】:

  • 最好使用Javascript。你一定要用 PHP 吗?
  • 标签之间的数据是动态的还是简单的html?
  • 解析标记,并使用选择器获取您想要的节点,然后以您想要的任何方式获取节点属性和值。 PHP 有 the DOMDocument 类,JavaScript 有 the DOMParser 对象。两者都会做得很好
  • 数据来自数据库

标签: php xml regex preg-replace


【解决方案1】:

好吧,我会咬人的。使用正则表达式解析 XML 就像试图用锤子砍树一样。当然,您最终可能会得到看起来可行的东西,但是您将花费太多时间编写代码来处理那些您使用的模式并不能完全解决问题的情况。
最好解析标记语言,如 XML 或 HTML。幸运的是,PHP 附带了几个可以做到这一点的工具:解析标记。刚开始,我将按照以下方式处理您拥有的数据:

$dom = new DOMDocument;
$dom->loadXML($xml);
$xpath = new DOMXpath($dom);
$nodes = $xpath->query('ml/locale');
$result = array();
foreach ($nodes as $node)
{
    $result[] = array(
        'val'        => $node->nodeValue,
        'nameAttr'   => $node->attributes->getNamedItem('name')->value
    );
}
var_dump($result);

你可以see here,它工作得很好。当然,这段代码根本没有准备好复制粘贴,但它应该足以让你开始。查看 xpaths 和 the DOMDocument API。值得花时间,保证!

为了帮助您更多,实际上,您应该先检查 $node-&gt;attributes-&gt;getNamedItem 的返回值,然后再访问其返回值的属性,因为它可以返回 null

foreach ($nodes as $node)
{
    $attribute = $node->attributes->getNamedItem('name') ? : null;
    $result[] = array(
        'val'  => $node->nodeValue,//empty string, or node contents/value
        'name' => $attribute ? $attribute->value : null//null, or the name attribute value
    );
}

这将是一种更可靠的方法。

【讨论】:

    【解决方案2】:

    在 PHP 中,您可以这样做:

    $rule = '/\<locale name\=\"([\w\.\-\_]*)\"\>([\w\.\-\_]*)\<\/locale\>/';
    

    然后你做:

    $array = array();
    
    preg_match_all($ruke, $string, $array);
    

    【讨论】:

    • 它给出了这个答案 Array ( [0] => Array ( [0] => ) [1] => Array ( [0] => en-US ) [2] => Array ( [0] => ) )
    • 好吧。请给我你传递给preg_match_all() 的字符串输入?
    • $string="125LKO.50C-SL137毫升>"
    【解决方案3】:

    您可以使用PHP: XmlReader 来读取您的Xml 文件的元素。一个正则表达式是可以的,你可以用它来接收你想要的东西,但是如果你想要更灵活,我更喜欢 PHP Xml Reader 类

    【讨论】:

      【解决方案4】:

      您可以使用正则表达式,这可能不是最好的解决方案,但它会是这样的:

      <locale.*>(.*)<\/locale>
      

      例如,用regex101 测试它,它可以工作。 像这样使用preg_match_all

      // $string is your DATA to search in 
      $string = '<ml><locale name="en-US">125LKO.50C-SL137</locale>
         <locale name="es-ES"></locale>
         </ml>';
      preg_match_all("/<locale.*>(.*)<\/locale>/", $string , $matches); // use preg_match only if searching for first occurrence only
      
      foreach($matches[0] as $match){ 
        echo $match; // first one is en and second is es  
      }  
      

      别忘了:用正则表达式解析 xml/html 通常不是一个好主意。尝试改用专用解析器。

      【讨论】:

      • 如果你的文本中只有一次出现这种情况,你必须使用 preg_match 而不使用 '_all' 然后 $matches 作为 $match 而不是 $matches[0]
      • 在这种情况下可以正常工作,但如果这些标签具有 html 内容,则会失败
      • 您在问题中没有提到这一点。这就是为什么你应该使用 DOM 解析器 :)
      【解决方案5】:

      你可以试试下面的 PHP 代码,

      <?php
      $mystring = <<< 'EOT'
      <ml><locale name="en-US">125LKO.50C-SL137</locale>
           <locale name="es-ES"></locale>
       </ml> 
      EOT;
      $regex =  '~(?m)<ml><locale\s*name="..\K-|(?<=>).+?(?=<)~';
      preg_match_all($regex, $mystring, $matches);
      var_dump($matches);
      ?>
      

      输出:

      array(1) {
        [0]=>
        array(2) {
          [0]=>
          string(1) "-"
          [1]=>
          string(16) "125LKO.50C-SL137"
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 2020-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        相关资源
        最近更新 更多