用正则表达式在PHP中提取HTML属性(Extract HTML attributes in PHP with regex)

HTML/CSS IT屋
百度翻译此文   有道翻译此文
问 题

I want to get HTML attributes from string with PHP but fail with:

$string = '<ul id="value" name="Bob" custom-tag="customData">';
preg_filter("/(\w[-\w]*)=\"(.*?)\"/", '$1', $string ); // returns "<ul id name custom-tag"
preg_filter("/(\w[-\w]*)=\"(.*?)\"/", '$1', $string ); // returns "<ul value Bob customData"

What I want to return is:

array(
  'id' => 'value',
  'name' => 'Bob',
  'custom-tag' => 'customData'
);
解决方案

HTML is not a regular language and cannot be correctly parsed with a regex. Use a DOM parser instead. Here's a solution using PHP's built-in DOMDocument class:

$string = '<ul id="value" name="Bob" custom-tag="customData">';

$dom = new DOMDocument();
$dom->loadHTML($string);

$result = array();

$ul = $dom->getElementsByTagName('ul')->item(0);
if ($ul->hasAttributes()) {
    foreach ($ul->attributes as $attr) {
        $name = $attr->nodeName;
        $value = $attr->nodeValue;    
        $result[$name] = $value;
    }
}

print_r($result);

Output:

Array
(
    [id] => value
    [name] => Bob
    [custom-tag] => customData
)

本文地址:IT屋 » Extract HTML attributes in PHP with regex

问 题

我想从PHP的字符串获取HTML属性,但失败:

  $ string ='< ul id = “value”name =“Bob”custom-tag =“customData”>'; 
preg_filter(“/(\ w [-\w] *)= \”(。*?)\“/”,'$ 1',$ string); //返回“< ul id name custom-tag”
preg_filter(“/(\ w [-\w] *)= \”(。*?)\“/”,'$ 1 ',$ string); //返回“< ul value Bob customData”


我想返回的是:



  array(
'id'=>'value',
'name'=>'Bob' ,
'custom-tag'=>'customData'
);

解决方案

HTML不是正规语言,无法正确解析与正则表达式。改用DOM解析器。以下是使用PHP内置的 DOMDocument 类的解决方案:



  $ string ='< ul id =“value”name =“Bob”custom-tag =“customData”>' ; 

$ dom = new DOMDocument();
$ dom-> loadHTML($ string);

$ result = array();

$ ul = $ dom-> getElementsByTagName('ul') - > item(0);
if($ ul-> hasAttributes()){
foreach($ ul-> attributes as $ attr){
$ name = $ attr-> nodeName;
$ value = $ attr-> nodeValue;
$ result [$ name] = $ value;
}
}

print_r($ result);


输出:



  
[id] =>
[name] => Bob
[custom-tag] => customData


本文地址:IT屋 » 用正则表达式在PHP中提取HTML属性

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起