<small id='Q0Ojk'></small><noframes id='Q0Ojk'>

<legend id='Q0Ojk'><style id='Q0Ojk'><dir id='Q0Ojk'><q id='Q0Ojk'></q></dir></style></legend>
    • <bdo id='Q0Ojk'></bdo><ul id='Q0Ojk'></ul>

    1. <tfoot id='Q0Ojk'></tfoot>

    2. <i id='Q0Ojk'><tr id='Q0Ojk'><dt id='Q0Ojk'><q id='Q0Ojk'><span id='Q0Ojk'><b id='Q0Ojk'><form id='Q0Ojk'><ins id='Q0Ojk'></ins><ul id='Q0Ojk'></ul><sub id='Q0Ojk'></sub></form><legend id='Q0Ojk'></legend><bdo id='Q0Ojk'><pre id='Q0Ojk'><center id='Q0Ojk'></center></pre></bdo></b><th id='Q0Ojk'></th></span></q></dt></tr></i><div id='Q0Ojk'><tfoot id='Q0Ojk'></tfoot><dl id='Q0Ojk'><fieldset id='Q0Ojk'></fieldset></dl></div>

        PHP 简单 HTML DOM 解析器:访问自定义属性

        时间:2024-04-13
          <tbody id='jn8Tq'></tbody>
            <tfoot id='jn8Tq'></tfoot>
              <bdo id='jn8Tq'></bdo><ul id='jn8Tq'></ul>
            • <legend id='jn8Tq'><style id='jn8Tq'><dir id='jn8Tq'><q id='jn8Tq'></q></dir></style></legend>

              1. <small id='jn8Tq'></small><noframes id='jn8Tq'>

                  <i id='jn8Tq'><tr id='jn8Tq'><dt id='jn8Tq'><q id='jn8Tq'><span id='jn8Tq'><b id='jn8Tq'><form id='jn8Tq'><ins id='jn8Tq'></ins><ul id='jn8Tq'></ul><sub id='jn8Tq'></sub></form><legend id='jn8Tq'></legend><bdo id='jn8Tq'><pre id='jn8Tq'><center id='jn8Tq'></center></pre></bdo></b><th id='jn8Tq'></th></span></q></dt></tr></i><div id='jn8Tq'><tfoot id='jn8Tq'></tfoot><dl id='jn8Tq'><fieldset id='jn8Tq'></fieldset></dl></div>
                • 本文介绍了PHP 简单 HTML DOM 解析器:访问自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想访问我添加到 HTML 文件中某些元素的自定义属性,这是 littleBox="somevalue" 属性的示例

                  I want to access a custom attribute that I added to some elements in an HTML file, here's an example of the littleBox="somevalue" attribute

                  <div id="someId" littleBox="someValue">inner text</div>
                  

                  以下不起作用:

                  foreach($html->find('div') as $element){
                   echo $element;
                   if(isset($element->type)){
                   echo $element->littleBox;
                     }
                  }
                  

                  我看到一篇有类似问题的文章,但由于某种原因我无法复制它.这是我尝试过的:

                  I saw an article with a similar problem, but I couldn't replicate it for some reason. Here is what I tried:

                  function retrieveValue($str){
                  if (stripos($str, 'littleBox')){//check if element has it
                  $var=preg_split("/littleBox="/",$str);
                  //echo $var[1];
                  $var1=preg_split("/"/",$var[1]);
                  echo $var1[0];
                  }
                  else
                  return false;
                  }
                  

                  每当我调用 retrieveValue() 函数时,什么都没有发生.$element (在上面的第一个 PHP 示例中)不是字符串吗?我不知道我是否错过了什么,但它没有返回任何东西.

                  When ever I call the retrieveValue() function, nothing happens. Is $element (in the first PHP example above) not a string? I don't know if I missed something but it's not returning anything.

                  这是完整的脚本:

                  <?php
                  require("../../simplehtmldom/simple_html_dom.php");
                  
                  if (isset($_POST['submit'])){
                  
                  $html = file_get_html($_POST['webURL']);
                  
                  // Find all images 
                  foreach($html->find('div') as $element){
                      echo $element;
                     if(isset($element->type)!= false){
                      echo retrieveValue($element);
                     }
                  }
                  }
                  
                  
                  function retrieveValue($str){
                  if (stripos($str, 'littleBox')){//check if element has it
                  $var=preg_split("/littleBox="/",$str);
                  //echo $var[1];
                  $var1=preg_split("/"/",$var[1]);
                  return $var1[0];
                  }
                  else
                  return false;
                  }
                  
                  ?>
                  
                  <form method="post">
                  Website URL<input type="text" name="webURL">
                  <br />
                  <input type="submit" name="submit">
                  </form>
                  

                  推荐答案

                  你试过了吗:

                  $html->getElementById("someId")->getAttribute('littleBox');
                  

                  您也可以使用 SimpleXML:

                  You could also use SimpleXML:

                  $html = '<div id="someId" littleBox="someValue">inner text</div>';
                  $dom = new DOMDocument;
                  $dom->loadXML($html);
                  $div = simplexml_import_dom($dom);
                  echo $div->attributes()->littleBox;
                  

                  我建议不要使用正则表达式来解析 html,但这部分不应该是这样的:

                  I would advice against using regex to parse html but shouldn't this part be like this:

                  $str = $html->getElementById("someId")->outertext;
                  $var = preg_split('/littleBox="/', $str);
                  $var1 = preg_split('/"/',$var[1]);
                  echo $var1[0];
                  

                  另请参阅此答案https://stackoverflow.com/a/8851091/1059001

                  这篇关于PHP 简单 HTML DOM 解析器:访问自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将操作按钮添加到在新窗口中打开的 Woocommerce 我的帐户订单 下一篇:禁止所有文件使用 Nginx 403

                  相关文章

                • <tfoot id='nbAZi'></tfoot>

                • <legend id='nbAZi'><style id='nbAZi'><dir id='nbAZi'><q id='nbAZi'></q></dir></style></legend>
                      <bdo id='nbAZi'></bdo><ul id='nbAZi'></ul>
                  1. <i id='nbAZi'><tr id='nbAZi'><dt id='nbAZi'><q id='nbAZi'><span id='nbAZi'><b id='nbAZi'><form id='nbAZi'><ins id='nbAZi'></ins><ul id='nbAZi'></ul><sub id='nbAZi'></sub></form><legend id='nbAZi'></legend><bdo id='nbAZi'><pre id='nbAZi'><center id='nbAZi'></center></pre></bdo></b><th id='nbAZi'></th></span></q></dt></tr></i><div id='nbAZi'><tfoot id='nbAZi'></tfoot><dl id='nbAZi'><fieldset id='nbAZi'></fieldset></dl></div>

                      <small id='nbAZi'></small><noframes id='nbAZi'>