Grabbing hidden inputs as a string (Using PHP Simple HTML DOM Parser)

ぃ、小莉子 提交于 2019-11-30 20:04:39

If you use DomDocument, you could do the following:

<?php
    $hidden_inputs = array();
    $dom = new DOMDocument('1.0');
    @$dom->loadHTMLFile('form_show.php');

    // 1. get all inputs
    $nodes = $dom->getElementsByTagName('input');

    // 2. loop through elements
    foreach($nodes as $node) {
        if($node->hasAttributes()) {
            foreach($node->attributes as $attribute) {
                if($attribute->nodeName == 'type' && $attribute->nodeValue == 'hidden') {
                    $hidden_inputs[] = $node;
                }
            }
        }
    } unset($node);

    // 3. loop through hidden inputs and print HTML
    foreach($hidden_inputs as $node) {
        echo "<pre>" . htmlspecialchars($dom->saveHTML($node)) . "</pre>";
    } unset($node);

?>

I don't use the SimpleDom (I always go whole-hog and use DOMDocument), but couldn't you do something like ->find('input[@type=hidden]')?

If the SimpleDOM doesn't allow that sort of selector, you could simply loop over the ->find('input') results and pick out the hidden ones by comparing the attributes yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!