Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取每个元素的宽度和高度';通过DOM或XPATH创建属性_Php_Php 5.3_Domdocument_Domxpath_Getattribute - Fatal编程技术网

Php 获取每个元素的宽度和高度';通过DOM或XPATH创建属性

Php 获取每个元素的宽度和高度';通过DOM或XPATH创建属性,php,php-5.3,domdocument,domxpath,getattribute,Php,Php 5.3,Domdocument,Domxpath,Getattribute,如何获取每个元素属性的宽度和高度 比如说, $dom = new DOMDocument; $dom->loadHTML('<div class="item" style="width:295px; height:210px; border:1px solid #000;"></div><div></div>'); foreach( $dom->getElementsByTagName('div') as $node ) {

如何获取每个元素属性的宽度和高度

比如说,

$dom = new DOMDocument;
$dom->loadHTML('<div class="item" style="width:295px; height:210px; border:1px solid #000;"></div><div></div>');

foreach( $dom->getElementsByTagName('div') as $node ) 
{
    $style = $node->getAttribute( 'style' );

    var_dump($style);
}
但这就是我想要的

  • 选择类名称仅为
    项的
    div
  • 仅获取
    295
    (宽度)和
    210
    (高度)
  • DOM有可能吗?还是XPATH

    编辑:

    我现在似乎成功地选择了带有类名的div

    $dom = new DOMDocument;
    $dom->loadHTML('<div class="item" style="width:295px; height:210px; border:1px solid #000;"></div><div></div>');
    
    $xpath = new DOMXpath($dom); 
    
    foreach ($xpath->query('//*[@class="item"]') as $node) {
    
        $style = $node->getAttribute( 'style' );
    
        var_dump($style);
    }
    
    $dom=新的DOMDocument;
    $dom->loadHTML(“”);
    $xpath=newdomxpath($dom);
    foreach($xpath->query('/*[@class=“item”]”)作为$node){
    $style=$node->getAttribute('style');
    var_dump($style);
    }
    
    这就是我想要的


    获取
    295
    (宽度)和
    210
    (高度)。

    如果不想使用正则表达式,可以使用简单的字符串函数提取所需内容。这里有一个例子,它很可能是可以改进的

    $width = 'width:'; $height = 'height:';
    // Adding whitespace will not affect the result
    $str = '    width:   295 px; height:   210 px; border:1px solid #000;';
    
    $width_str = strstr( $str, $width);
    echo 'Width: "' . trim( substr( $width_str, strlen( $width), stripos( $width_str, 'px;') - strlen( $width))) . '"';
    
    echo "\n";
    
    $height_str = strstr( $str, $height);
    echo 'Height: "' . trim( substr( $height_str, strlen( $height), stripos( $height_str, 'px;') - strlen( $height))) . '"';
    
    当然,您可以用字符串文本替换
    $width
    $height
    变量,并删除对
    strlen()
    的调用,因为它们是常量整数

    您可以使用此函数获取
    宽度
    高度
    或其他样式值,只需调用:


    $width=GetStyleValue($img->getAttribute(“样式”),'width')

    我记得,样式属性是“kebab-cased”——依我看,它们都应该是小写的,所有单词都用连字符分隔。给你

    通过使用DOM解析器将
    item
    类的
    div
    元素作为目标,您已经展示了一个不错的选择

    解析样式声明的下一步同样是最可靠的,但是如果您对这一步不感兴趣,那么使用有识别模式的两个
    preg_match()
    调用应该可以让您保持良好的状态

    对于那些不知道可能会影响此任务结果的边缘案例的人,我正在为其中一个div添加一个
    title
    属性,该属性将愚弄不适当的模式或不了解DOM的技术,并且出于同样的原因添加一个
    行高
    声明。这些示例将捕获许多可能无法解析DOM的“快捷方式”解决方案

    正则表达式模式必须匹配整个单词
    高度
    宽度
    。我的模式将检查这些单词是否位于字符串的开头,或者前面是否有分号,然后是零个或多个空白字符。一旦找到其中一个单词,下一个非白色字符必须是冒号。然后,在再次允许零个或多个空白字符后,我使用
    \K
    来“忘记”所有以前匹配的字符,然后只返回所需的数字字符作为“完整字符串匹配”(元素
    [0]

    代码:()


    将回答有关XPath使用特定类属性获取div的部分。您需要常规的字符串函数或CSS解析器来分解内联样式。这不是DOM的域。解答如何使用regexon a sidenote解析内联样式,以获得div的style属性和item的class属性。您还可以执行
    //div[@class=“item”]/@style
    。这将返回一个DOMAttr。我的意思是,这个答案当然应该是DV'ed!毕竟,它正在展示最佳/直接实践,识别麻烦的边缘案例,包括教育性解释,并通过在线演示证明其成功处理。为什么Stackoverflow希望此类内容出现在页面顶部?
    $width = 'width:'; $height = 'height:';
    // Adding whitespace will not affect the result
    $str = '    width:   295 px; height:   210 px; border:1px solid #000;';
    
    $width_str = strstr( $str, $width);
    echo 'Width: "' . trim( substr( $width_str, strlen( $width), stripos( $width_str, 'px;') - strlen( $width))) . '"';
    
    echo "\n";
    
    $height_str = strstr( $str, $height);
    echo 'Height: "' . trim( substr( $height_str, strlen( $height), stripos( $height_str, 'px;') - strlen( $height))) . '"';
    
    function GetStyleValue($style, $type){
    $value = false;
    $all = explode(';',$style);
    foreach($all as $part){
        $temp = explode(':', $part);
        if(trim($temp[0]) == $type){
            $value = (int)trim($temp[1]);
        }       
    }
    return $value;}
    
    $html = <<<HTML
    <div class="items">
        <div class="item" title="Checking for bad parsing. height:666px; width:666px;" style="width:295px; height:210px; border:1px solid #000;"></div>
        <div></div>
        <div class="item" style="line-height:14pt; border:1px solid #000; height :420px; width: 590px;"></div>
    </div>
    HTML;
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXpath($dom); 
    foreach ($xpath->query('//div[@class="item"]/@style') as $node) {
        $style = $node->nodeValue;
        echo "The height integer: " , preg_match('~(?:^|;)\s*height\s*:\s*\K\d+~', $style, $h) ? $h[0] : '';
        echo "\n";
        echo "The width integer: " , preg_match('~(?:^|;)\s*width\s*:\s*\K\d+~', $style, $w) ? $w[0] : '';
        echo "\n---\n";
    }
    
    The height integer: 210
    The width integer: 295
    ---
    The height integer: 420
    The width integer: 590
    ---