【问题标题】:Getting the href attribute and text of certain kind of links获取某种链接的 href 属性和文本
【发布时间】:2014-04-26 02:00:50
【问题描述】:

在这四个链接中:

<img border="0" src="imagenes/flech.gif" width="6" height="8">

<a href="escuchar-baladas-de-Albano_Y_Romina_Power.html">Albano Y Romina Power</a><br>
<img border="0" src="imagenes/flech.gif" width="6" height="8">

<a href="escuchar-baladas-de-Armando_Manzanero.html">Armando Manzanero</a><br>

<a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html">
<img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>

<a href="Musica-Baladas-Alternativas.html">Baladas Alternativas</a><br>

我试图首先捕获三个链接的 href 值和文本,而忽略第四个链接,换句话说,我试图得到这个:

escuchar-baladas-de-Albano_Y_Romina_Power.html    Albano Y Romina Power
escuchar-baladas-de-Armando_Manzanero.html    Armando Manzanero
musica-Merengue-de-Banda_Cuisillos.html    Banda Cuisillos

我试图充分利用前三个有imagenes/flech.gif 的事实,这样就忽略了第四个,imagenes/flech.gif 的顺序不同。 Here 是我尝试解决它的地方,我达到了 href 但包括第四个。

感谢您的帮助

【问题讨论】:

  • 必填answer,但如果你只需要解析这4个链接,假设它们永远不会改变,我想答案很快就会出现。
  • 您使用哪种语言?
  • 只是显示的 4 个链接。
  • 第二个&lt;img&gt;不包含任何链接,对吗?
  • @Tuga 前两个与我放置的链接中的顺序相同

标签: php html regex


【解决方案1】:

你应该使用 html 解析器而不是正则表达式,试试这个:

<?php

$html = <<< EOF
<img border="0" src="imagenes/flech.gif" width="6" height="8">

<a href="escuchar-baladas-de-Albano_Y_Romina_Power.html">Albano Y Romina Power</a><br>
<img border="0" src="imagenes/flech.gif" width="6" height="8">

<a href="escuchar-baladas-de-Armando_Manzanero.html">Armando Manzanero</a><br>

<a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html">
<img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>

<a href="Musica-Baladas-Alternativas.html">Baladas Alternativas</a><br>
EOF;


$dom = new DOMDocument();
@$dom->loadHTML($html);

# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {

    $url =  $link->getAttribute('href');
    $text = preg_replace('/[\r\n]/sm', '', $link->nodeValue); // remove line breaks

    //if doesn't contain the banned words...
    if (!preg_match('/(Baladas Alternativas|another text to filter)/sm', $text)) {
        echo $url ." ".$text. "\n";
    } 

}
?>

演示
http://ideone.com/5QX83x

资源
http://htmlparsing.com/php.html

【讨论】:

  • @user2495207 如果 html 发生更改,您仍然会得到结果 :) 如果我的回答对您有帮助,请考虑接受它作为正确答案,tks!
【解决方案2】:

此代码将获得前 3 个链接

$a='<img border="0" src="imagenes/flech.gif" width="6" height="8"><a href="escuchar-baladas-de-Albano_Y_Romina_Power.html">Albano Y Romina Power</a><br><img border="0" src="imagenes/flech.gif" width="6" height="8"><a href="escuchar-baladas-de-Armando_Manzanero.html">Armando Manzanero</a><br><a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html"><img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br><a href="Musica-Baladas-Alternativas.html">Baladas Alternativas</a><br>';

preg_match_all('/<a.*?href="(.+?)">(?:<img.*\d+">)?(.+?)<\/a>/',$a,$match);


echo $match[1][0] . "  " . $match[2][0]."<br>";
echo $match[1][1] . "  " . $match[2][1]."<br>";
echo $match[1][2] . "  " . $match[2][2]."<br>";

【讨论】:

  • 但是$match[2][2]&lt;img border="0" src="imagenes/flech.gif" width="6" height="8"&gt;Banda Cuisillos 而不是Banda Cuisillos。无论如何谢谢
  • 我知道你已经选择了答案,但请检查我编辑的代码
  • $match[2][2]Baladas Alternativas 而不是 Banda Cuisillos
  • 代码已编辑,没有看到返回另一个乐队
猜你喜欢
  • 2023-03-14
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2011-11-25
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多