DOMDocument Tag nav invalid in Entity

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

DOMDocument Tag nav invalid in Entity

文章 yehlu »

http://stackoverflow.com/questions/6090 ... html5-tags


down vote
accepted
No, there is no way of specifying a particular doctype to use, or to modify the requirements of the existing one.

Your best workable solution is going to be to disable error reporting with libxml_use_internal_errors:

代碼: 選擇全部

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML('...');
libxml_clear_errors();
or use html5lib instead of the DOM extension (note that this is described as 'currently unmaintained since April 9, 2013!).
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Php : Get name and value of all input tags on a page with Do

文章 yehlu »

http://www.binarytides.com/php-get-nam ... document/

代碼: 選擇全部

/*
    Generic function to fetch all input tags (name and value) on a page
Useful when writing automatic login bots/scrapers
*/
function get_input_tags($html)
{
    $post_data = array();
     
    // a new dom object
    $dom = new DomDocument; 
     
    //load the html into the object
    $dom->loadHTML($html); 
    //discard white space
    $dom->preserveWhiteSpace = false; 
     
    //all input tags as a list
    $input_tags = $dom->getElementsByTagName('input'); 
 
    //get all rows from the table
    for ($i = 0; $i < $input_tags->length; $i++) 
    {
        if( is_object($input_tags->item($i)) )
        {
            $name = $value = '';
            $name_o = $input_tags->item($i)->attributes->getNamedItem('name');
            if(is_object($name_o))
            {
                $name = $name_o->value;
                 
                $value_o = $input_tags->item($i)->attributes->getNamedItem('value');
                if(is_object($value_o))
                {
                    $value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
                }
                 
                $post_data[$name] = $value;
            }
        }
    }
     
    return $post_data;
}
 
/*
    Usage
*/
 
error_reporting(~E_WARNING);
$html = file_get_contents("https://accounts.google.com/ServiceLoginAuth");
 
echo "<pre>";
print_r(get_input_tags($html));
echo "</pre>";
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Get all <option>s from <select> using getElementById

文章 yehlu »

http://stackoverflow.com/questions/1095 ... lementbyid

3
down vote
accepted
You have to use the DOM-API to retrieve the data you want. Since the <select> element is not that complex you can fetch all the <options>-nodes using getElementsByTagName:

代碼: 選擇全部


$select = $this->Dom->getElementById('DDteam');
$options = $select->getElementsByTagName('option');

$optionInfo = array();
foreach($options as $option) {
    $value = $option->getAttribute('value');
    $text = $option->textContent;

    $optionInfo[] = array(
        'value' => $value,
        'text' => $text,
    );
}

var_dump($optionInfo);

回覆文章

回到「PHP」