<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 解析器:访问自定义属性

        PHP Simple HTML DOM Parser: Accessing custom attributes(PHP 简单 HTML DOM 解析器:访问自定义属性)
          <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 解析器:访问自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Converting string to MySQL timestamp format in php(在php中将字符串转换为MySQL时间戳格式)
                  datetime to timestamp(日期时间到时间戳)
                  PHP timestamp date to user timezone(PHP时间戳日期到用户时区)
                  Converting TIMESTAMP to unix time in PHP?(在 PHP 中将 TIMESTAMP 转换为 unix 时间?)
                  Convert ISO 8601 to unixtimestamp(将 ISO 8601 转换为 unixtimestamp)
                  Finding days between 2 unix timestamps in php(在php中查找2个unix时间戳之间的天数)
                    <tbody id='nbAZi'></tbody>

                  • <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>
                        • <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'>