I need to get "option" values from a site. But there are more than one "select". How do I get "option" values by "Select name" value . (get all option value by select name=ctl02$dlOgretimYillari)
<select name="ctl02$dlOgretimYillari" onchange="javascript:setTimeout('__doPostBack(\'ctl02$dlOgretimYillari\',\'\')', 0)" id="ctl02_dlOgretimYillari" class="NormalBlack">
<option selected="selected" value="-40">2016-2017</option>
</select>
My Code :
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$options_value = array();
$options_name = array();
$options_selected = array();
foreach($dom->getElementsByTagName('option') as $option) {
array_push($options_value, $option->getAttribute('value'));
array_push($options_selected, $option->getAttribute('selected'));
array_push($options_name, $option->nodeValue);
}
get all option value by select name=ctl02$dlOgretimYillari
The solution using DOMXPath::query method:
$content = '<select name="ctl02$dlOgretimYillari" onchange="javascript:setTimeout(\'__doPostBack(\'ctl02$dlOgretimYillari\',\'\')\', 0)" id="ctl02_dlOgretimYillari" class="NormalBlack"> <option selected="selected" value="-40">2016-2017</option> </select>';
$doc = new DOMDocument();
libxml_use_internal_errors();
$doc->loadXML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($doc);
$nodes = $xpath->query("//select[#name='ctl02\$dlOgretimYillari']/option/#value");
// outputting first option value
print_r($nodes->item(0)->nodeValue);
The output:
-40
For additional condition: How do I get the value of the option text?
...
$nodes = $xpath->query("//select[#name='ctl02\$dlOgretimYillari']/option/text()");
...
#RomanPerekhrest 's answer definitely solved the initial question!
But in my case I needed to get all the selected values, so here's the solution for anyone with the same problem:
$nodes = $xpath->query("//select[#name='ctl02\$dlOgretimYillari']/option[#selected]/#value");
// Printing
foreach ($nodes as $node) {
print_r($node->nodeValue);
}
Related
For my project I'm reading an external website which has used the same ID twice. I can't change that.
I need the content from the second appearance of that ID but my code just results the first one and does not see the second one.
Also a count to $data results 1 but not 2.
I'm desperate. Does anyone have an idea how to access the second ID 'hours'?
<?PHP
$url = 'myurl';
$contents = file_get_contents($url);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile($url);
$data = $dom->getElementById("hours");
echo $data->nodeValue."\n";
echo count($data);
?>
As #rickdenhaan points out, getElementById always returns a single element which is the first element that has that specific value of id. However you can use DOMXPath to find all nodes which have a given id value and then pick out the one you want (in this code it will find the second one):
$url = 'myurl';
$contents = file_get_contents($url);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile($url);
$xpath = new DOMXPath($dom);
$count = 0;
foreach ($xpath->query("//*[#id='hours']") as $node) {
if ($count == 1) echo $node->nodeValue;
$count++;
}
As #NigelRen points out in the comments, you can simplify this further by directly selecting the second input in the XPath i.e.
$node = $xpath->query("(//*[#id='hours'])[2]")[0];
echo $node->nodeValue;
Demo on 3v4l.org
I have a xml which the format is like this:
<root>
<a>1</a>
<b>2</b>
<c></c>
</root>
This is the code I have tried:
$to = 3;
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->load("../xxx.xml");
$xpath = new DOMXPath($dom);
$query = "/root/*[position()=$to]";
$nodes = $xpath->query($query);
$node = $nodes[0];
$dom->removeChild($node);
$dom->save("../xxx.xml", LIBXML_NOEMPTYTAG);
How can I delete the tag with name "c" ?
Oh lord, the problem was lie under
$dom->removeChild($node);
should be
$node->parentNode->removeChild($node);
in order to delete a node, you have to get back to the parent node and then it will take the action..I think, this is just my two cents. if someone understand well, feel free to correct me
This question already has answers here:
How to retrieve comments from within an XML Document in PHP
(4 answers)
Closed 8 years ago.
I am trying to retrieve content from a p element in this page. As you can see, in the source code there is a paragraph with the content i want:
<p id="qb"><!--
QBlastInfoBegin
Status=READY
QBlastInfoEnd
--></p>
Actually i want to take the value of the Status.
Here is my PHP code.
#$dom->loadHTML($ncbi->ncbi_request($params));
$XPath = new DOMXpath($dom);
$nodes = $XPath->query('//p[#id="qb"]');
$node = $nodes->item(0)->nodeValue;
var_dump($node))
that returns
["nodeValue"]=> string(0) ""
Any idea ?
Thanks!
Seems that to get comment values you need to use //comment()
I'm not too familiar with XPaths so am not too sure on the exact syntax
Sources: https://stackoverflow.com/a/7548089/723139 / https://stackoverflow.com/a/1987555/723139
Update: with working code
<?php
$data = file_get_contents('http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?RID=UY5PPBRH014&CMD=Get');
$dom = new DOMDocument();
#$dom->loadHTML($data);
$XPath = new DOMXpath($dom);
$nodes = $XPath->query('//p[#id="qb"]/comment()');
foreach ($nodes as $comment)
{
var_dump($comment->textContent);
}
I checked up the site, and it seems you are after the comment inside, you need to add comment() on your xpath query. Consider this example:
$contents = file_get_contents('http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?RID=UY5PPBRH014&CMD=Get');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$comment = $xpath->query('//p[#id="qb"]/comment()')->item(0)->nodeValue;
echo '<pre>';
print_r($comment);
Outputs:
QBlastInfoBegin
Status=READY
QBlastInfoEnd
I have some HTML like this:
<dd class="price">
<sup class="symbol">$</sup><span class="dollars">58</span><sup class="cents">.00</sup>
</dd>
What's the xpath to get $58.00 back as one string?
I'm using PHP:
$xpath = '?????';
$result = $xml->xpath($xpath);
echo $result[0]; // want this to show $58.00, possible?
These are valid in your case, check for more detail the links below;
$html = '<dd class="price">
<sup class="symbol">$</sup><span class="dollars">58</span><sup class="cents">.00</sup>
</dd>';
$dom = new DOMDocument();
$dom->loadXML($html);
$xpt = new DOMXpath($dom);
foreach ($xpt->query('//dd[#class="price"]') as $node) {
// outputs: $58.00
echo trim($node->nodeValue);
}
// or
$xml = new SimpleXMLElement($html);
$res1 = $xml->xpath('//dd[#class="price"]/sup');
$res2 = $xml->xpath('//dd[#class="price"]/span');
// outputs: $58.00
printf('%s%s%s', (string) $res1[0], (string) $res2[0], (string) $res1[1]);
DOMDocument
DOMXPath
SimpleXMLElement
data() will return all contents inside the current context. Try
//dd/data()
You haven't shown us your code so I don't know what platform you're using. If you have something that can evaluate non-node XPath expressions, then you can use this:
string(//dd[#class = 'price'])
if not, you can select the node,
//dd[#class = 'price']
and the API you're using should have a way of getting the inner text value of the selected node.
How do I get the value of an input field like the one below where it does not have an ID attribute using PHP's DOMDocument?
<input type="text" name="make" value="Toyota">
XPath makes it simple, assuming that's the only text input with "make" as its name:
$dom = new DOMDocument();
$dom->loadHTML(...);
$xp = new DOMXpath($dom);
$nodes = $xp->query('//input[#name="make"]');
$node = $nodes->item(0);
$car_make = $node->getAttribute('value');
If there's more than one input with that particular field name on the page (which is entirely possible), then you'll have to do some extra work to narrow down WHICH of those multiple inputs you want.
$dom = new DOMDocument();
$dom->loadHTML($result);
$xpath = new DOMXpath($dom);
$node = $xpath->query('//input[#name="token"]/attribute::value');
$token = $node->item(0)->nodeValue;