将HTML转换成PHP数组

时间:2019-06-16 10:49:16

标签: php

我有一个在$html变量中也包含HTML的字符串:

'Here is some <a href="#">text</a> which I do not need to extract but then there are 
<figure class="class-one">
    <img src="/example.jpg" alt="example alt" class="some-image-class">
    <figcaption>example caption</figcaption>
</figure>

And another one (and many more)
<figure class="class-one some-other-class">
    <img src="/example2.jpg" alt="example2 alt">
</figure>'

我想提取所有<figure>元素及其包含的所有内容(包括其属性和其他html元素),并将其放入PHP数组中,这样我会得到类似的东西:

    $figures = [
        0 => [
            "class" => "class-one",
            "img" => [
                "src" => "/example.jpg",
                "alt" => "example alt",
                "class" => "some-image-class"
            ],
            "figcaption" => "example caption"
        ],
        1 => [
            "class" => "class-one some-other-class",
            "img" => [
                "src" => "/example2.jpg",
                "alt" => "example2 alt",
                "class" => null
            ],
            "figcaption" => null
        ]];

到目前为止,我已经尝试过:

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();

$figures = array();
foreach ($figures as $figure) {
    $figures['class'] = $figure->getAttribute('class');
    // here I tried to create the whole array but I can't seem to get the values from the HTML 
    // also I'm not sure how to get all html-elements within <figure>   
} 

这里是Demo

2 个答案:

答案 0 :(得分:2)

 $doc = new \DOMDocument();
      $doc->loadHTML($html);

      $figure = $doc->getElementsByTagName("figure"); // DOMNodeList Object

      //Craete array to add all DOMElement value
      $figures = array();
      $i= 0;
      foreach($figure as $item) { // DOMElement Object

        $figures[$i]['class']= $item->getAttribute('class');
        //DOMElement::getElementsByTagName— Returns html tag
        $img = $item->getElementsByTagName('img')[0];

        if($img){
            //DOMElement::getAttribute — Returns value of attribute
            $figures[$i]['img']['src'] = $img->getAttribute('src');

            $figures[$i]['img']['alt'] = $img->getAttribute('alt');
            $figures[$i]['img']['class'] = $img->getAttribute('class');
        }
        //textContent - use to get the text of tag
        if($item->getElementsByTagName('figcaption')[0]){
            $figures[$i]['figcaption'] = $item->getElementsByTagName('figcaption')[0]->textContent;
        }

        $i++;
      }

      echo "<pre>";
      print_r($figures);
      echo "</pre>";

答案 1 :(得分:2)

这是应该使您到达想要的位置的代码。我在觉得有帮助的地方添加了评论:

<?php

$htmlString = 'Here is some <a href="#">text</a> which I do not need to extract but then there are <figure class="class-one"><img src="/example.jpg" alt="example alt" class="some-image-class"><figcaption>example caption</figcaption></figure>And another one (and many more)<figure class="class-one some-other-class"><img src="/example2.jpg" alt="example2 alt"></figure>';

//Create a new DOM document
$dom = new DOMDocument;

//Parse the HTML.
@$dom->loadHTML($htmlString);

//Create new XP
$xp = new DOMXpath($dom);

//Create empty figures array that will hold all of our parsed HTML data
$figures = array();

//Get all <figure> elements
$figureElements = $xp->query('//figure');

//Create number variable to keep track of our $figures array index
$figureCount = 0;

//Loop through each <figure> element
foreach ($figureElements as $figureElement) {
    $figures[$figureCount]["class"] = trim($figureElement->getAttribute('class'));
    $figures[$figureCount]["img"]["src"] = $xp->query('//img', $figureElement)->item($figureCount)->getAttribute('src');
    $figures[$figureCount]["img"]["alt"] = $xp->query('//img', $figureElement)->item($figureCount)->getAttribute('alt');

    //Check that an img class exists, otherwise set the value to null. If we don't do this PHP will throw a NOTICE.
    if (boolval($xp->evaluate('//img', $figureElement)->item($figureCount))) {
        $figures[$figureCount]["img"]["class"] = $xp->query('//img', $figureElement)->item($figureCount)->getAttribute('class');
    } else {
        $figures[$figureCount]["img"]["class"] = null;
    }

    //Check that a <figcaption> element exists, otherwise set the value to null
    if (boolval($xp->evaluate('//figcaption', $figureElement)->item($figureCount))) {
        $figures[$figureCount]["figcaption"] = $xp->query('//figcaption', $figureElement)->item($figureCount)->nodeValue;
    } else {
        $figures[$figureCount]["figcaption"] = null;
    }

    //Increment our $figureCount so that we know we can create a new array index.
    $figureCount++;
}

print_r($figures);
?>