如何使用 php 将标签的所有属性从字符串解析为数组?
How to parse all attributes of tag from string into array using php?
我有一个 html 字符串,例如...
<match id="18" srs="ICC Womens World Cup Qualifier, 2010" mchDesc="BANW vs PMGW" mnum="4th Match">
使用php我如何split/decode/parse这个字符串作为一个可访问的对象(键值对)比如....
array(
"id"=>"18",
"srs"=>"ICC Womens World Cup Qualifier, 2010",
"mchDesc"=>"BANW vs PMGW",
"mnum"=>"4th Match"
);
输出:
Array
(
[id] => 18
[srs] => ICC Womens World Cup Qualifier, 2010
[mchDesc] => BANW vs PMGW
[mnum] => 4th Match
)
这应该有效。
(\w+)\=\"([a-zA-Z0-9 ,.\/&%?=]+)\"
代码PHP:
<?php
$re = '/(\w+)\=\"([a-zA-Z0-9 ,.\/&%?=]+)\"/m';
$str = '<match id="18" srs="ICC Womens World Cup Qualifier, 2010" mchDesc="BANW vs PMGW" mnum="4th Match">
';
preg_match_all($re, $str, $matches);
$c = array_combine($matches[1], $matches[2]);
print_r($c);
输出:
Array
(
[id] => 18
[srs] => ICC Womens World Cup Qualifier, 2017
[mchDesc] => BANW vs PMGW
[mnum] => 4th Match, Group B
[type] => ODI
[vcity] => Colombo
[vcountry] => Sri Lanka
[grnd] => Colombo Cricket Club Ground
[inngCnt] => 0
[datapath] => google.com/j2me/1.0/match/2017/
)
正则表达式 101:https://regex101.com/r/lyMmKF/7
使用DOMDocument
and DOMAttr
:
$str = '<match id="18" srs="ICC Womens World Cup Qualifier, 2010" mchDesc="BANW vs PMGW" mnum="4th Match">';
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($str);
$result = [];
foreach($dom->getElementsByTagName('match')->item(0)->attributes as $attr) {
$result[$attr->name] = $attr->value;
}
print_r($result);
主要优点是它不关心属性值是否包含在单引号或双引号之间(或根本没有引号),以及等号前后是否有空格。
我有一个 html 字符串,例如...
<match id="18" srs="ICC Womens World Cup Qualifier, 2010" mchDesc="BANW vs PMGW" mnum="4th Match">
使用php我如何split/decode/parse这个字符串作为一个可访问的对象(键值对)比如....
array(
"id"=>"18",
"srs"=>"ICC Womens World Cup Qualifier, 2010",
"mchDesc"=>"BANW vs PMGW",
"mnum"=>"4th Match"
);
输出:
Array
(
[id] => 18
[srs] => ICC Womens World Cup Qualifier, 2010
[mchDesc] => BANW vs PMGW
[mnum] => 4th Match
)
这应该有效。
(\w+)\=\"([a-zA-Z0-9 ,.\/&%?=]+)\"
代码PHP:
<?php
$re = '/(\w+)\=\"([a-zA-Z0-9 ,.\/&%?=]+)\"/m';
$str = '<match id="18" srs="ICC Womens World Cup Qualifier, 2010" mchDesc="BANW vs PMGW" mnum="4th Match">
';
preg_match_all($re, $str, $matches);
$c = array_combine($matches[1], $matches[2]);
print_r($c);
输出:
Array
(
[id] => 18
[srs] => ICC Womens World Cup Qualifier, 2017
[mchDesc] => BANW vs PMGW
[mnum] => 4th Match, Group B
[type] => ODI
[vcity] => Colombo
[vcountry] => Sri Lanka
[grnd] => Colombo Cricket Club Ground
[inngCnt] => 0
[datapath] => google.com/j2me/1.0/match/2017/
)
正则表达式 101:https://regex101.com/r/lyMmKF/7
使用DOMDocument
and DOMAttr
:
$str = '<match id="18" srs="ICC Womens World Cup Qualifier, 2010" mchDesc="BANW vs PMGW" mnum="4th Match">';
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($str);
$result = [];
foreach($dom->getElementsByTagName('match')->item(0)->attributes as $attr) {
$result[$attr->name] = $attr->value;
}
print_r($result);
主要优点是它不关心属性值是否包含在单引号或双引号之间(或根本没有引号),以及等号前后是否有空格。