如何在PHP中使用Dom Document找到Dom元素的样式属性?

时间:2017-01-05 09:06:07

标签: php html dom domdocument

我使用PHP Dom Document从文件中读取HTML Dom元素。

public interface SomeInterface {
    //--------------------------------------------------------------------
    String getSomething1(String input1, String input2) throws SomeException;

    //--------------------------------------------------------------------
    Map<String, String> getSomething2(String input1) throws SomeException;

    //--------------------------------------------------------------------
    String getSomething3(SomeOtherInterface input3) throws SomeException;

    //--------------------------------------------------------------------
    Map<String, String> getSomething4(Map<String, SomeOtherInterface> input1,
        SomeOtherMoreInterface input2, String input3) throws SomeException;

    //--------------------------------------------------------------------
    SomeOtherInterface[] getSomething5(String[] input1) throws SomeException;
}

我可以通过 ID 来定位元素,并读取元素的内容等等。

但我无法找到它的风格属性。

因为我需要克隆它或使用新参数进行更改。

如何阅读其样式属性并进行更改?

有可能吗?

还有其他解决方案吗?

2 个答案:

答案 0 :(得分:3)

你可以试试这个:如果CSS在同一页面上,你可以试试这个:

    $html = file_get_contents('testing.html');
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $div = $dom->getElementById('test');
    if ($div->hasAttributes()) {
        foreach ($div->attributes as $attr) {
            $name = $attr->nodeName;
            $value = $attr->nodeValue;
            if( strcmp($name,"class") == 0){
                $x=getStyle($dom->textContent,".$value{","}");
                echo "<pre>";
                print_r($x);
             }
             if( strcmp($name,"id") == 0){
                $idCss=getStyle($dom->textContent,"#$value{","}");
                echo "<pre>";
                print_r($idCss);
             }
         }
    }


     function getStyle($string, $start, $end){
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $len = strpos($string, $end, $ini);
        return substr($string, $ini, $len);
    }

更新您可以使用的样式。

$div->setAttribute('style', 'background-color:blue;'); 
     echo $dom->saveHTML(); exit;. 

<强> testing.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div id="test" class="stl" style="width:100px;">

</div>
</body>
</html>

<style>
.stl{
background-color:green;
}

</style>

<强>输出:

.stl{
background-color:green;
}

如果您只寻找风格,可以这样做:

$html = file_get_contents('testing.html');
        $dom = new DOMDocument();
        $dom->loadHTML($html);
    $div = $dom->getElementById('test');
    if ($div->hasAttributes()) {
        echo $div->getAttribute('style');//width:100px;
    }

答案 1 :(得分:2)

DOM解析器只允许您读取DOM中的数据。

它不会加载CSS,将其应用于DOM,然后让您访问元素的计算属性。

要实现这一点,您需要采用不同的方法并使用将DOM应用于DOM的工具。最常见的是SeleniumPhantomJSPHP bindings可用)(它们也会执行JavaScript)。