I'm not finding a way to retrieve all elements that have an attribute ec:edit. I've only found examples getting namespaced elements, but not attributes.
And there is also no result when searching the attributes with attr() or hasAttr().
dbpedia example:
foreach ($qp->branch()->find('foaf|page') as $img) {
print $img->attr('rdf:resource') . PHP_EOL;
}
rdf file sample:
<dbpprop:artist rdf:resource="http://dbpedia.org/resource/The_Beatles" />
But this won't retrieve any results:
$edits = $htmldocument->find('div[mc|edit];
foreach ($edits as $key => $value) {
echo $value->attr('mc:edit');
}
sample data:
<div mc:edit="stuff"> // etc
I get nothing.
Ok, lambdas solve everything:
find('div')->filterLambda('return qp($item)->hasAttr("mc:edit");');
Related
Good day,
I'm trying to get a specific value of an XML file in this example I need to get a
productidentifier in each product that has the tag <b221> set to 03 something like this:
<productidentifier><b221>03</b221><b244>9783672461027</b244></productidentifier>
where I can get then the <b244> tag to store the EAN.
The problem is that specific items have sometimes more then one productidentifier for example:
<productidentifier><b221>02</b221><b244>3672461024</b244></productidentifier>
<productidentifier><b221>03</b221><b244>9783672461027</b244></productidentifier>
So far I tried to use:
foreach ($xml->product as $item) {
$item->productidentifier->b221
}
Which will always return me the first productidenfitier in the list
foreach ($xml->product as $item) {
$identifier = $item->productidentifier->xpath('//productidentifier');
dd($identifier);
}
Which returns me all the product identifiers in the list:
https://i.stack.imgur.com/mq0NE.png
How to do it?
Thank you for reading.
You would need to extend your XPath expression to include a check that the <b221> element is 03. This code searches for these and outputs the <b244> value
foreach ( $xml->xpath('//productidentifier[b221="03"]') as $product ) {
echo $product->b244.PHP_EOL;
}
I'm using the Kraken API to get a list of available currency pairings.
Here is the endpoint: https://api.kraken.com/0/public/AssetPairs
$kraken_coins =
file_get_contents('https://api.kraken.com/0/public/AssetPairs');
$kraken_coins = json_decode($kraken_coins, true);
foreach ($kraken_coins['result'] as $coin) {
echo $coin . "<br>";
}
I'm trying to extract the first element inside of "result", however each of these elements are named differently so I'm not sure how to target it. For example, the first two currency pairings returned are "BCHEUR" and "BCHUSD".
So with the loop I have above, it just echos "Array" repeatedly... not what I'm going for.
How do I target that element so I can loop through each one? Thanks!
Since, the json structure is:
You need to use:
foreach ($array as $key => $value) {
}
In this case:
foreach ($kraken_coins['result'] as $coin => $coindata) {
echo $coin . "<br>";
}
I am trying to extract the data stored in a list. The troubling piece is the "custom-data" entity.
<li id="myid" custom-data="123456789" class="search-query">
...lots of child elements
I get all the string data with querying DomXPath object for "search-query" and than extract it with this the code below.
$id = $exampleXPath->query("//li[#class='search-query']");
foreach ($id as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo "$node->nodeValue<br>";
}
}
I would like to also get the value of "custom-data" of each "search-query" object, but I don't how to get it. Any ideas?
You already have reference to all "search-query" elements, so getting the attribute should be as straightforward as calling getAttribute("attribute_name") on each element:
$custom_data = $element->getAttribute("custom-data");
I am using the SimpleHTMLDom library in PHP and i am able to fetch the nodes from my HTML data.
For example i have a html data as below :
<html>
<body>
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
</body>
<html>
and i am using the parsing code as :
foreach($html->find('td') as $element)
echo $element->plaintext . '<br>';
But this code gives result as :
One<br/>
Two<br/>
Three<br/>
What i want is that i want the result "One" in say variable $one, "Two" in $two and "Three" in $three.
The problem is that how can i get a particular table column element in a particular variable. I want to traverse irrespective of ID or class. Like I want to get like 3rd occurrence of the tag in the HTML data. Thanks. The SIMPLEHTMLDOM.sourceforge.com manual is not understandable.
The find() method accepts a second optional parameter: the index of the element.
echo $html->find('td', 2);
Output:
Three
nb1: if index is set, it returns the Nth element object, otherwise, return an array of object.
nb2: the parameter is zero based, it means that the the index of the first element is 0
You can also use the array to access to your element:
$elements = $html->find('td');
echo $elements[2];
Output:
Three
doc: http://simplehtmldom.sourceforge.net/manual_api.htm
echo getElementByIndex(3); //whatever element you are looking for.
function getElementByIndex($indexOfElement) {
$i = 1; //or 0 depending on starting list index
foreach($html->find('td') as $element) {
$i++;
if ($i == $indexOfElement)
return $element->plaintext . '<br>';
}
}
The simple_html_dom library is great for getting known attributes, but is there a way to get a list of all the attributes for an element?
For example, if I have:
<div id="test" custom1="custom" custom2="custom">
I can easily get the id:
$el = $html->find('div');
$id = $el->id;
But, is it possible to get custom1 and custom2 if they are not known ahead of time? Ideally, the solution would produce an array of the NVP's for all attributes (id, custom1, custom2).
$el->attr is an associated array of tag=>value s
You can use get_object_vars to get an associative array, and then loop over them.
$attrs = get_object_vars($el);
foreach($attrs as $key=>$value) {
}