1

Of these four links:

<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>

I'm trying to capture the href value and the text of the link of the three first, leaving out the fourth link, in other words i'm trying to get this:

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

I was trying to make the most of the fact that the three first have imagenes/flech.gif and that way leave out the fourth, the thing that imagenes/flech.gif isn't in the same order. Here is my attempt to solve it where i get up to the href but include the fourth.

Thanks for any help

Community
  • 1
  • 1
user2495207
  • 861
  • 2
  • 10
  • 15

2 Answers2

1

You should use an html parser and not a regex, try this:

<?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";
    } 

}
?>

DEMO
http://ideone.com/5QX83x

RESOURCES
http://htmlparsing.com/php.html

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • @user2495207 if the html changes you still get the results :) If my answer helped you, please consider accepting it as the correct answer, tks! – Pedro Lobito Apr 26 '14 at 12:43
0

this code will get the first 3 links

$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>";
Sirius_Black
  • 471
  • 3
  • 11