Php DOMNode travelling - php

I'm trying to parse an HTML document, and get text values from tags, but the problem is that the tags don't contain any special attributes or have some id's to target them.
The only thing that can be anchored to - is another static text, used as Labels.
The source page code looks similar to this
<tr>
<td>
<span>
Some text to link to
</span>
</td>
<td>
<span>
THE text to get
</span>
</td>
</tr>
/*****************Parser Page Script*************************/
$file = "src/src.htm";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
/********* Page that Processes *********/
//Pattern for regEx
$pattern = "/Some text to link to/";
$elements = $doc->getElementsByTagName('td');
if (!is_null($elements)) {
foreach ($elements as $node){
$text = $node->textContent;
if(preg_match($pattern, $text, $matches)){
echo "<pre>";
print_r($node);
echo "</pre>";
}
}
}
How to get the nextSibling value for searched td if the result is [nextSibling] => (object value omitted)?

A possibility is to use Xpath. Example xpath: /table/tr/td/span
$file = "src/src.htm";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$elements = $xpath->query('/table/tr/td/span');
if(!empty($elements))
{
foreach($elements as $element)
{
echo $element->nodeValue;
}
}

Related

PHP DomDocument get text after tag

I have this in my php file.
<?php
$str = '<div>
<p>Text</p>
I need this text...
<p>next p</p>
... and this
</div>
';
$dom=new DomDocument();
$dom->loadHTML($str);
$p = $dom->getElementsByTagName('p');
foreach ($p as $item) {
echo $item->nodeValue;
}
This gives me the correct text for the p tags, but I also need the the text between the p tags ("I need this text...", "...and this").
Anyone know how to get the text after the p tag?
Best
Use DOMXPath:
$xpath = new DOMXpath($domDocument);
foreach ($xpath->query('//div/text()') as $textNode) {
echo $textNode->nodeValue;
}

How can i get the text from a child node with php DOMDocument

I've been writing a php code to get information from a site, so far i was able to get the href attribute, but i cant find a way to get the text from the child node "span", can someone help me?
html- >
<a class="js-publication" href="publication/247931167">
<span class="publication-title">An approach for textual authoring</span>
</a>
This is how i am currently able to get the href ->
#$dom->loadHTMLFile($curPage);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
$class_ = $element->getAttribute('class');
if (0 !== strpos($class_, 'js-publication')) {
$href = $element->getAttribute('href');
if(0 === stripos($href,'publication/')){
echo $href;//link para a publicação;
echo "\n";
}
}
}
You can use DOMXpath
$html = <<< LOL
<a class="js-publication" href="publication/247931167">
<span class="publication-title">An approach for textual authoring</span>
</a>
LOL;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//a[#class='js-publication']") as $element){
echo $element->getAttribute('href');
echo $element->textContent;
}
//publication/247931167
//An approach for textual authoring
Or without the for loop, if you just want one element :
echo $xpath->query("//a[#class='js-publication']/span")[0]->textContent;
echo $xpath->query("//a[#class='js-publication']")[0]->getAttribute('href');
Ideone Demo

how to use preg_match_all to remove the <a> tag

$content = preg_replace("~(<a href=\"(.*)\">\w+)~iU", '', $content);
$ok = preg_replace("~(</a>)~iU", '', $content);
echo $ok;
I need to control the $content...
I want to remove all link in the $content....
even <img xxxx> all to remove A tag Just save <img xxx>...
How can I do?
I need edit the REGEX??
why I just can del the first one
You can replace the anchors with their contents using DOMDocument:
$html = <<<'EOS'
<img src="http://example.com">
<img src="http://example.com">
EOS;
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a') as $anchor) {
$fragment = $doc->createDocumentFragment();
// collecting the child nodes
foreach ($anchor->childNodes as $node) {
$fragment->appendChild($node);
}
// replace anchor with all child nodes
$anchor->parentNode->replaceChild($fragment, $anchor);
}
echo $doc->saveHTML();

get value of <h2> of html page with PHP DOM?

I have a var of a HTTP (craigslist) link $link, and put the contents into $linkhtml. In this var is the HTML code for a craigslist page, $link.
I need to extract the text between <h2> and </h2>. I could use a regexp, but how do I do this with PHP DOM? I have this so far:
$linkhtml= file_get_contents($link);
$dom = new DOMDocument;
#$dom->loadHTML($linkhtml);
What do I do next to put the contents of the element <h2> into a var $title?
if DOMDocument looks complicated to understand/use to you, then you may try PHP Simple HTML DOM Parser which provides the easiest ever way to parse html.
require 'simple_html_dom.php';
$html = '<h1>Header 1</h1><h2>Header 2</h2>';
$dom = new simple_html_dom();
$dom->load( $html );
$title = $dom->find('h2',0)->plaintext;
echo $title; // outputs: Header 2
You can use this code:
$linkhtml= file_get_contents($link);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($linkhtml); // loads your html
$xpath = new DOMXPath($doc);
$h2text = $xpath->evaluate("string(//h2/text())");
// $h2text is your text between <h2> and </h2>
You can do this with XPath: untested, may contain errors
$linkhtml= file_get_contents($link);
$dom = new DOMDocument;
#$dom->loadHTML($linkhtml);
$xpath = new DOMXpath($dom);
$elements = $xpath->query("/html/body/h2");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}

How do I extract this value using PHP Dom

I do have html file this is just a prt of it though...
<div id="result" >
<div class="res_item" id="1" h="63c2c439b62a096eb3387f88465d36d0">
<div class="res_main">
<h2 class="res_main_top">
<img
src="/ff/gigablast.com.png"
alt="favicon for gigablast.com"
width=16
height=16
/>
<a
href="http://www.gigablast.com/"
rel="nofollow"
>
Gigablast
</a>
<div class="res_main">
<h2 class="res_main_top">
<img
src="/ff/ask.com.png"
alt="favicon for ask.com"
width=16
height=16
/>
<a
href="http://ask.com/" rel="nofollow"
>
Ask.com - What's Your Question?
</a>....
I want extract only url address (for example: http://www.gigablast.com and http://ask.com/ - there are atleast 10 urls in that html) from above using PHP Dom Document..I know up to this but dont know how to move ahead??
$doc = new DomDocument;
$doc->loadHTMLFile('urllist.html');
$data = $doc->getElementById('result');
then what?? this is inside tag hence I cant use $data->getElementsByTagName() here!!
Using XPath to narrow down the field to a elements inside the <div class="res_main"> element:
$doc = new DomDocument();
$doc->loadHTMLFile('urllist.html');
$xpath = new DomXpath($doc);
$query = '//div[#class="res_main"]//a';
$nodes = $xpath->query($query);
$urls = array();
foreach ($nodes as $node) {
$href = $node->getAttribute('href');
if (!empty($href)) {
$urls[] = $href;
}
}
This solves the problem of picking up all the <a> elements inside of the document, since it allows you to filter only the ones you want (since you don't care about navigation links, etc)...
You can call getElementsByTagName on a DOMElement object:
$doc = new DomDocument;
$doc->loadHTMLFile('urllist.html');
$result = $doc->getElementById('result');
$anchors = $result->getElementsByTagName('a');
$urls = array();
foreach ($anchors as $a) {
$urls[] = $a->getAttribute('href');
}
If you want to get image sources as well, that would be easy to add.
If you are just trying to extract the href attribute of all a tags in the document (and the <div id="result"> doesn't matter, you could use this:
$doc = new DomDocument;
$doc->loadHTMLFile('urllist.html');
$anchors = $doc->getElementsByTagName('a');
$urls = array();
foreach($anchors as $anchor) {
$urls[] = $anchor->attributes->href;
}
// $urls is your collection of urls in the original document.

Categories