I am trying to extract multiple URLs from HTML file with regex.
There are other URLs in the file, do the only pattern i have is "tableentries." and ""
HTML code example:
<tr class="tableentries2">
<td>
Click Here
</td>
PHP I wrote:
$html = "value of the code above"
if(preg_match_all('/<td>.*</td>/', $html, $match)){
foreach($match[0] as $x){
echo $x . "<br>";
}}
Why not just look for href values? (Updated because the edited code now has quotation marks.)
preg_match_all('/href="([^\s"]+)/', $html, $match);
Then the URI would be in $match[1][0].
You really shouldn't use regex to parse HTML. DOMDocument is actually very easy to use for this type of thing. here is a simple example.
<?php
error_reporting(E_ALL);
$html = "
<table>
<tr>
<td>
<a href='http://www.test1-1.com'>test1-1</a>
</td>
<td>
<a href='http://www.test1-2.com'>test1-2</a>
</td>
<td>
<a href='http://www.test1-3.com'>test1-3</a>
</td>
</tr>
<tr>
<td>
<a href='http://www.test2-1.com'>test2-1</a>
</td>
<td>
<a href='http://www.test2-2.com'>test2-2</a>
</td>
<td>
<a href='http://www.test2-3.com'>test2-3</a>
</td>
</tr>
</table>";
$DOM = new DOMDocument();
//load the html string into the DOMDocument
$DOM->loadHTML($html);
//get a list of all <A> tags
$a = $DOM->getElementsByTagName('a');
//loop through all <A> tags
foreach($a as $link){
//echo out the href attribute of the <A> tag.
echo $link->getAttribute('href').'<br />';
}
?>
This would output:
http://www.test1-1.com
http://www.test1-2.com
http://www.test1-3.com
http://www.test2-1.com
http://www.test2-2.com
http://www.test2-3.com
<?php
preg_match_All("#<a\s[^>]*href\s*=\s*[\'\"]??\s*?(?'path'[^\'\"\s]+?)[\'\"\s]{1}[^>]*>(?'name'[^>]*)<#simU", $html, $hrefs, PREG_SET_ORDER);
foreach ($hrefs AS $urls){
print $urls['path']."<br>";
}
?>
Related
Can anyone help in parsing this part of an HTML site? I use php and PHP:DOM
I would like to get the Klassifikation and Schlagwörter in one php string.
How is this done?
Thanks
<tr style="display:table-row;">
<td id="TREFWOORD" class="onOffLink"></td>
<td class="rec_lable"><div>
<span>Schlagwörter</span><span>: </span>
</div></td>
<td class="rec_title"><div>
<span>*</span><span><a class="
link_gen
" href="MAT=/NOMAT=T/REL?PPN=106189719">Recht</a></span><span>
</span><span><a href="http://"
target=""><img src="http://"
alt="Subject" title="Subject" class="img_link"></a></span><span> / </span>
<span><a class="
link_gen
" href="MAT=/NOMAT=T/CMD?
ACT=SRCHA&IKT=5040&TRM=Wo%CC%88rterbuch">Wörterbuch</a></span>
</div></td>
</tr>
<tr style="display:table-row;">
<td></td>
<td class="rec_lable"><div><span>Klassifikation: </span></div></td>
<td class="rec_title"><div>
<span>Basisklassifikation: </span><span><a class="
link_gen
" target=""><img
src="http://" alt="Subject"
title="Subject" class="img_link"></a></span>
</div></td>
</tr>
I tried this without success:
<?php
$url='http://...'
$easycurlcmd=sprintf("curl '%s' -o ./libbvhtml.txt", $url);
printf("Execute: CURL1 ".$easycurlcmd."\n");
exec($easycurlcmd);
$html=file_get_contents('./libbvhtml.txt');
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$rec_lable = $xpath->query("//tr/*[contains(#class, rec_lable')]/div/span[1]");
echo $rec_lable->item(0)->nodeValue; // Schlagwörter
echo $rec_lable->item(1)->nodeValue; // Klassifikation
The reason was that curl must be defined with the redirect option.
Thanks to all.
You need to use DOMDocument::loadHTML to parsing HTML and use DOMXPath::query to searching in DOM.
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$rec_lable = $xpath->query("//tr/*[contains(#class, 'rec_lable')]/div/span[1]");
echo $rec_lable->item(0)->nodeValue; // Schlagwörter
echo $rec_lable->item(1)->nodeValue; // Klassifikation
Check result in demo
I'm trying to extract img src and the text of the TDs inside the div id="Ajax" but i'm unable to extract the img with my code. It just ignores the img src. How can i extract also the img src and add it in the array?
HTML:
<div id="Ajax">
<table cellpadding="1" cellspacing="0">
<tbody>
<tr id="comment_1">
<td>20:28</td>
<td class="color">
</td>
<td class="last_comment">
Text<br/>
</td>
</tr>
<tr id="comment_2">
<td>20:25</td>
<td class="color">
</td>
<td class="comment">
Text 2<br/>
</td>
</tr>
<tr id="comment_3">
<td>20:24</td>
<td class="color">
<img src="http://url.ext/img/image02.jpeg" alt="img alt 2"/>
</td>
<td class="comment">
Text 3<br/>
</td>
</tr>
<tr id="comment_4">
<td>20:23</td>
<td class="color">
<img src="http://url.ext/img/image01.jpeg" alt="img alt"/>
</td>
<td class="comment">
Text 4<br/>
</td>
</tr>
</div>
PHP:
$html = file_get_contents($url);
$doc = new DOMDocument();
#$doc->loadHTML($html);
$contentArray = array();
$doc = $doc->getElementById('Ajax');
$text = $doc->getElementsByTagName ('td');
foreach ($text as $t)
{
$contentArray[] = $t->nodeValue;
}
print_r ($contentArray);
Thanks.
You're using $t->nodeValue to obtain the content of a node. An <img> tag is empty, thus has nothing to return. The easiest way to get the src attribute would be XPath.
Example:
$html = file_get_contents($url);
$doc = new DOMDocument();
#$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$expression = "//div[#id='Ajax']//tr";
$nodes = $xpath->query($expression); // Get all rows (tr) in the div
$imgSrcExpression = ".//img/#src";
$firstTdExpression = "./td[1]";
foreach($nodes as $node){ // loop over each row
// select the first td node
$tdNodes = $xpath->query($firstTdExpression ,$node);
$tdVal = null;
if($tdNodes->length > 0){
$tdVal = $tdNodes->item(0)->nodeValue;
}
// select the src attribute of the img node
$imgNodes = $xpath->query($imgSrcExpression,$node);
$imgVal = null;
if($imgNodes ->length > 0){
$imgVal = $imgNodes->item(0)->nodeValue;
}
}
(Caution: Code may contain typos)
I have searched online and thought this would work but it doesn't for some reason. I'm trying to extract a hyperlink that only displays it's URL from a HTML. I'm only trying to extract the URL within the td align="center". Here is a sample of the HTML doc I'm trying to extract:
<td>
Aug 17
</td>
<td>
FT
</td>
<td align="right">
Arsenal ruby
</td>
**<td align="center">**
1-3
</td>
<td>Aston Villa</td>
<td style="text-align:right;">60,003</td>
And here is my PHP code to extract it from the td align="center":
<?php
//$searchURL = "site";
include 'simple_html_dom.php';
$site = 'website';
$html = file_get_html($site);
$tabledata = array();
// Find all TD tags with "align=center"
foreach($html->find('td[align=center]') as $e)
echo $e->href . '<br>';
?>
I know the code works because the code can extract everything if it is just the td within the barracks.
So you have identified the <td> elements themselves, but you did not go down to the next nesting level to grab the href from the <a> elements. You might do that like this:
foreach($html->find('td[align=center]') as $e)
echo $e->children(0)->href . '<br>';
Use the DOM and Xpath:
Select all td elements in the document
//td
Only if the align attribute equals "center"
//td[#align="center"]
Get the a sub elements
//td[#align="center"]//a
Get the href attribute nodes of that a elements
//td[#align="center"]//a/#href
Source example:
$html = <<<'HTML'
<td>
FT
</td>
<td align="right">
Arsenal ruby
</td>
**<td align="center">**
1-3
</td>
<td>Aston Villa</td>
<td style="text-align:right;">60,003</td>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$nodes = $xpath->evaluate('//td[#align="center"]//a/#href');
foreach ($nodes as $node) {
var_dump($node->value);
}
You selected the td element. The anchor element is the child of the td element.
// Find all TD tags with "align=center"
foreach($html->find('td[align=center]') as $e)
echo $e->firstChild()->getAttribute('href') . '<br>';
Hi I am very new to this World of DOMDocument,Im still learning and looking for xpath query use in DOMDocument.The html sometimes changes so a preg_match is not a good idea. .I need to get the values from a html file.This is the part of html i want to get. I would be happy if you could help me..
<?php
$doc = new DOMDocument();
#$doc->loadHTML('<table cellspacing="0" cellpadding="0" align="center" class="results">
<tr class="header" bgcolor="#0000FF">
<td>
</td>
<td>Name/AKAs</td>
<td>Age</td>
<td>Location</td>
<td>Possible Relatives</td>
</tr>
<tr>
<td>1.</td>
<td>
<a class="LN" href=""><b>Iron, Man E</b></a>
</td>
<td align="center">54</td>
<td>
Canada, AK<br />
California, AK<br />
</td>
<td>
</td>
<td>
View Details
</td>
</tr>
<tr><td>2.</td>
<td>
<a class="LN" href=""><b>Bat, Man E</b></a></td>
<td align="center">26</td>
<td>
Gotham, IA
<br /></td>
<td>
View Details</td></tr>
</table>');
$xpath = new DOMXPath($doc);
$xquery = '//a[#class="LN"]';
$links = $xpath->query($xquery);
foreach ($links as $el) {
echo strip_tags($doc->saveHTML($el)).'<br/>';
}
?>
How do I get the following value? I can only get Iron, Man E, and Bat, Man E
Iron, Man E | 54 | Canada, AK;California, AK
Bat, Man E | 26 | Gotham, IA
My Answer is not about DomDocument Query but can solve your problem easily.
There is a Library named SIMPLEHTMLDOM ! You can do great things with it.
Example :
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
Full Documentation (Power of this Lib) is Here.
Try this,
$xquery = '//a'; // you will get all anchor tags now
$links = $xpath->query($xquery);
foreach ($links as $el) {
echo strip_tags($doc->saveHTML($el)).'<br/>';
}
Try this to get in a single line,
$xpath = new DOMXPath($doc);
$xquery = '//tr[td[a]]';
$links = $xpath->query($xquery);
foreach ($links as $el) {
echo strip_tags($doc->saveHTML($el)).'<br/>';
}
i need to get only image that in current node and not in child nodes
i want to get only green/yellow/red/black images without not_important.gif image
i can use query './/table/tr/td/img'
but i need it inside loop
<?php
/////////////////////////////////////////////////////////////////////
$html='
<table>
<tr>
<td colspan="2">
<span>
<img src="not_important.gif" />
</span>
<img src="green.gif" />
</td>
</tr>
<tr>
<td>
<span>yellow</span>
<img src="yellow.gif" />
</td>
<td>
<span>red</span>
<img src="red.gif" />
</td>
</tr>
</table>
<table>
<tr>
<td>
<span>
<img src="not_important.gif" />
</span>
<img src="black.gif" />
</td>
</tr>
</table>
';
/////////////////////////////////////////////////////////////////////
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
/////////////////////////////////////////////////////////////////////
$query = $xpath->query('.//table/tr/td');
for( $x=0,$results=''; $x<$query->length; $x++ )
{
$x1=$x+1;
$image = $query->item($x)->getELementsByTagName('img')->item(0)->getAttribute('src');
$results .= "image $x1 is : $image<br/>";
}
echo $results;
/////////////////////////////////////////////////////////////////////
?>
can i do it through $query->item()->
i tried has_attributes and getElementsByTagNameNS and getElementById
but i failed ::
Replace:
$image = $query->item($x)->getELementsByTagName('img')->item(0)->getAttribute('src');
...with:
$td = $query->item($x); // grab the td element
$img = $xpath->query('./img',$td)->item(0); // grab the first direct img child element
$image = $img->getAttribute('src'); // grab the source of the image
In other words, use the XPath object again to query, but now for ./img, relative to the context node you provide as the second argument to query(). The context node being one of the elements (td) of the earlier result.
The query //table/tr/td/img should work just fine as the unwanted images all reside in <span> elements.
Your loop would look like
$images = $xpath->query('//table/tr/td/img');
$results = '';
for ($i = 0; $i < $images->length; $i++) {
$results .= sprintf('image %d is: %s<br />',
$i + 1,
$images->item($i)->getAttribute('src'));
}