使用简单的Html dom解析器通过特定搜索文本获取链接

时间:2016-05-24 10:16:23

标签: php html-parsing

我想通过简单的html dom解析器PHP

获取标题中特定文本的链接

html代码

<h3 class="title"><a href="0.html" class="title">  keyword1 </a></h3>

如果标题是&#34;关键字1&#34;如何获取链接?

我的代码

$html = file_get_html($url);


foreach($html->find('h3[class=title]') as $e){

if(isset($e)){
foreach($e->find('keywords1') as $ee)

   foreach($html->find('a') as $eee){
 $d = $eee;




print_r ($d)."<br>";
}

2 个答案:

答案 0 :(得分:1)

此代码查找包含a

keyword1标记
$html = file_get_html($url);
foreach($html->find('h3[class=title] a[plaintext*=keyword1]') as $a) 
   echo $a->href . "\n";  //  0.html

要确定它是您正在寻找的,您还可以测试

trim($a->innertext) == 'keyword1'

UPD:

如果我正确理解你想要的东西

echo substr($url, 0, strrpos($url, '/')) . '/' . $a->href;

答案 1 :(得分:0)

我认为您正在寻找此方法 preg_match_all
http://php.net/manual/en/function.preg-match-all.php

相关问题