dom x path to grab href value [duplicate] - php

This question already has answers here:
How to extract a node attribute from XML using PHP's DOM Parser
(3 answers)
Closed 3 years ago.
I have the following html
<div class="logo">***® text.<sup>TM</sup></div>
I would like to get the value of href with php dom xpath, how would I accomplish that?
This is what I have tried:
$anchors = $domXpath->query("//div[#class='logo']/a");
foreach($anchors as $a)
{
print $a->nodeValue." - ".$a->getAttribute("href")."<br/>";
}

Here is the solution.
$xpath = new DOMXpath($dom);
$link = $xpath->query('//div[#class="logo"]/a');
$link->getAttribute('href')

Related

php replace id element content in string [duplicate]

This question already has answers here:
PHP DOMDocument replace content by div id
(1 answer)
How do you parse and process HTML/XML in PHP?
(31 answers)
Find an element by id and replace its contents with php
(3 answers)
Change innerHTML of a php DOMElement [duplicate]
(10 answers)
DOM change element content
(1 answer)
Closed 3 years ago.
a bit stuck, is it possible to use PHP to update/change html element contents.
So replace only 'Hello World' in the string below based off the the id name.
$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
Perhaps using preg_replace, just want to leave all other content in there, eg. class, data, etc.
$html = preg_replace('<h1 id="item">????</h1>', 'New Content', $html);
Thanks
May be below code will help
$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
$elementId = "item";
$newString = "Replace World";
$dom = new DOMDocument();
$dom->loadHTML($html);
$belement = $dom->getElementById("$elementId");
$oldString = $belement->nodeValue;
$newHTML = str_replace("$oldString","$newString","$html");
echo $newHTML;
Note: Please change $elementId and $newString with original values

String between php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I've got something like this:
$string = '<some code before><div class="abc">Something written here</div><some other code after>'
What I want is to get what is within the div and output it:
Something written here
How can I do that in php? Thanks in advance!
You would use the DOMDocument class.
// HTML document stored in a string
$html = '<strong><div class="abc">Something written here</div></strong>';
// Load the HTML document
$dom = new DOMDocument();
$dom->loadHTML($html);
// Find div with class 'abc'
$xpath = new DOMXPath($dom);
$result = $xpath->query('//div[#class="abc"]');
// Echo the results...
if($result->length > 0) {
foreach($result as $node) {
echo $node->nodeValue,"\n";
}
} else {
echo "Empty result set\n";
}
Read up on the expression syntax for XPath to customize your DOM searches.

Extract text between 2 strings? [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have a huge HTML page that contains multiple data like this
<td style="font-size:24px;" bgcolor="#F0F0F0" width="60%">
<strong>ID:Full Name:email#email.com:Mobile:Country</strong>
</td>
I want to extract the data between the tags which is ID:Full Name:email#email.com:Mobile:Country
So what would be the regex or any custom PHP Function?
PS: The above code is being repeated multiple times in a page and I want all that data to be stored in an array.
As of others has said, you can just use DOMDocument and DOMXpath. Like this:
$html = '<td style="font-size:24px;" bgcolor="#F0F0F0" width="60%"> <strong>ID:Full Name:email#email.com:Mobile:Country</strong></td>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$text = $xpath->query('//td/strong')->item(0)->nodeValue;
echo $text; // ID:Full Name:email#email.com:Mobile:Country

Can not get Xpath to fetch a nodeList [duplicate]

This question already has answers here:
Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?
(2 answers)
Closed 8 years ago.
libxml_use_internal_errors(true);
$url = 'http://thepiratebay.is/browse/200/0/7';
$html = file_get_contents($url);
$dom = new \DOMDocument();
$dom->loadHTML($html);
$x = new \DOMXPath($dom);
$nodeList = $x->query('/html/body/div[2]/div[2]/table/tbody/tr');
foreach ($nodeList as $node) {
die(var_dump($node));
}
Gives me the error:
"Invalid argument supplied for foreach()"
Not sure why xpath doesn't work on that domain?
If I'm right you'd like to get all the titles in that table. I'd suggest an easier, yet more specific XPath query, i.e.
$nodeList = $x->query('//div[#class="detName"]');
See it in action

php DOMDocument How to convert node value to string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I get an element's serialised HTML with PHP's DOMDocument?
PHP + DOMDocument: outerHTML for element?
I am trying to extract all img tags from a string. I am using:
$domimg = new DOMDocument();
#$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');
foreach ($images_all as $image) {
// do something
}
I want to put the src= values or even the complete img tags into an array or string.
Use saveXML() or saveHTML() on each node to add it to an array:
$img_links = array();
$domimg = new DOMDocument();
$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');
foreach ($images_all as $image) {
// Append the XML or HTML of each to an array
$img_links[] = $domimg->saveXML($image);
}
print_r($img_links);
You could try a DOM parser like simplexml_load_string. Take a look at a similar answer I posted here:
Needle in haystack with array in PHP

Categories