【问题标题】:Adding attr tag in html via PHP通过 PHP 在 html 中添加 attr 标签
【发布时间】:2018-06-27 02:40:41
【问题描述】:

我想在我的 HTML 对象中添加另一个属性和值, 但是,它正在替换当前值。

这是我迄今为止在我的 PHP 文件中编写的代码。

function htmlEncode ( $html ){

$html = preg_replace('~>\s+<~', '><', $html);
$html = html_entity_decode($html);

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

foreach($dom->getElementsByTagName('*') as $div){

    foreach ($div->attributes as $attr) {

        if($div->nodeName == "h2"){
            $class = $attr->nodeName;
            $className = $attr->nodeValue;
            $div->setAttribute("aria-label", $div->nodeValue);  

            $result = [
                 "tagName" => $div->nodeName, 
                 "value" => $div->nodeValue, 
                 $class=> $className, 
                 $attr->nodeName => $attr->nodeValue
            ];
        } else {
            $result[] = [
                 "tagName" => $div->nodeName, 
                 "value" => $div->nodeValue,
                 $attr->nodeName => $attr->nodeValue
           ];
        }
    }       
}

$json = json_encode($result, JSON_UNESCAPED_UNICODE);

return $json;
}

但是当我运行代码时

echo json_encode($attr->nodeName);

我得到了两个属性:

“类”“aria-label”

【问题讨论】:

    标签: php html html-object


    【解决方案1】:

    试试下面的代码。 所做的更改:

    1. $class[] = array();
    
    2. $class[$attr->nodeName] = $attr->nodeValue;
    3. Removed `class` from results
    4. Added 
                foreach($class as $key=>$classValues){
                    if(!empty($classValues)){
                         $result[$key] = $classValues;     
                    }
                 }
    

    完整代码:

    function htmlEncode ( $html ){
    
    $html = preg_replace('~>\s+<~', '><', $html);
    $html = html_entity_decode($html);
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    
    foreach($dom->getElementsByTagName('*') as $div){
        $class[] = array();
        foreach ($div->attributes as $attr) {
            if($div->nodeName == "h2"){
                $class[$attr->nodeName] = $attr->nodeValue;
                $div->setAttribute("aria-label", $div->nodeValue);
    
                $result = [
                     "tagName" => $div->nodeName, 
                     "value" => $div->nodeValue,
                     $attr->nodeName => $attr->nodeValue
                ];
                foreach($class as $key=>$classValues){
                     if(!empty($classValues)){
                            $result[$key] = $classValues;     
                     }
                }
            } else {
                $result[] = [
                     "tagName" => $div->nodeName, 
                     "value" => $div->nodeValue,
                     $attr->nodeName => $attr->nodeValue
               ];
            }
        }       
    }
    
    $json = json_encode($result, JSON_UNESCAPED_UNICODE);
    
    return $json;
    }
    

    【讨论】:

      【解决方案2】:

      尝试添加新行

      $div->setAttribute("class", $div->nodeValue);
      $div->setAttribute("aria-label", $div->nodeValue);
      

      【讨论】:

      • 我明白这一点,但我想要的是返回这两个属性,而不是用“class”替换“aria-label”。
      • 哦,在这种情况下,请查看此链接:stackoverflow.com/questions/24250820/…
      猜你喜欢
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      相关资源
      最近更新 更多