Php 基于URL的结尾使用simple_html_dom获取特定URL

Php 基于URL的结尾使用simple_html_dom获取特定URL,php,html,simple-html-dom,Php,Html,Simple Html Dom,我需要根据URL的结尾使用simple_html_dom获取URL。URL没有使其唯一的特定类。它唯一的独特之处在于它以一组特定的数字结尾。我就是想不出正确的语法来获取特定的URL然后打印出来 有什么帮助吗 例如: <table class="findList"> <tr class="findResult odd"> <td class="primary_photo"> <a href="/title/tt0080487/?ref_=fn_al_tt_

我需要根据URL的结尾使用simple_html_dom获取URL。URL没有使其唯一的特定类。它唯一的独特之处在于它以一组特定的数字结尾。我就是想不出正确的语法来获取特定的URL然后打印出来

有什么帮助吗

例如:

<table class="findList">
<tr class="findResult odd"> <td class="primary_photo"> <a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a> </td>


这是表格开头的代码。第一个href就是我想要的。该表继续提供更多链接等,但这与我想要的内容无关

您只需使用DOMdocument即可

<?php 
$html = '
<table class="findList">
<tr class="findResult odd"> 
    <td class="primary_photo"> 
        <a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a> 
    </td>
';


$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
    if($td->getAttribute('class') == 'primary_photo'){
        $a = $td->getElementsByTagName('a')->item(0)->getAttribute('href');
    }
}
echo $a; // title/tt0080487/?ref_=fn_al_tt_1



//Or if your looking to get the img tag
$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
    if($td->getAttribute('class') == 'primary_photo'){
        $a = $td->getElementsByTagName('img')->item(0)->getAttribute('src');
    }
}

echo $a; // http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg
?>

您只需使用DOMdocument即可

<?php 
$html = '
<table class="findList">
<tr class="findResult odd"> 
    <td class="primary_photo"> 
        <a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a> 
    </td>
';


$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
    if($td->getAttribute('class') == 'primary_photo'){
        $a = $td->getElementsByTagName('a')->item(0)->getAttribute('href');
    }
}
echo $a; // title/tt0080487/?ref_=fn_al_tt_1



//Or if your looking to get the img tag
$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
    if($td->getAttribute('class') == 'primary_photo'){
        $a = $td->getElementsByTagName('img')->item(0)->getAttribute('src');
    }
}

echo $a; // http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg
?>

假设您的html位于名为“tables.html”的文件中,这将起作用。它读取文件,找到所有的“a”链接,将它们放入一个数组中,第一个($anchors[0])就是您想要的。然后使用$anchors[0]->href从中获取href

$html = new simple_html_dom(); 

$html->load_file('tables.html');

$anchors = $html->find("a");

echo $anchors[0]->href;

假设您将html放在一个名为“tables.html”的文件中,这将起作用。它读取文件,找到所有的“a”链接,将它们放入一个数组中,第一个($anchors[0])就是您想要的。然后使用$anchors[0]->href从中获取href

$html = new simple_html_dom(); 

$html->load_file('tables.html');

$anchors = $html->find("a");

echo $anchors[0]->href;

对于a href以1结尾的第一个a:

$dom->find('a[href$="1"]', 0);

对于a href以1结尾的第一个a:

$dom->find('a[href$="1"]', 0);

好故事,你能举个例子吗好故事,你能举个例子吗。