第 1 頁 (共 1 頁)
DOMDocument Tag nav invalid in Entity
發表於 : 2016-09-09 09:03:02
由 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!).
Php : Get name and value of all input tags on a page with Do
發表於 : 2016-09-09 10:07:52
由 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>";
Get all <option>s from <select> using getElementById
發表於 : 2016-09-13 10:57:22
由 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);