Ajay
Ajay

Reputation: 11

php regular expression for matching anchor tags

go to the source of this page : www.songs.pk/indian/7days.html

there will be only eight links which start with http://link1

for example : <a href="http://link1.songs.pk/song1.php?songid=2792">Tune Mera Naam Liya</a>

i want a php regular expression which matches the

http://link1.songs.pk/song1.php?songid=2792

and

Tune Mera Naam Liya

Thanks.

Upvotes: 1

Views: 491

Answers (3)

Jan Turoň
Jan Turoň

Reputation: 32922

here you go

preg_match_all('~<a .*href="(http://link1\..*)".*>(.*)</a>~Ui',$str,$match,PREG_SET_ORDER);
print_r($match);

Upvotes: 0

alex
alex

Reputation: 490617

Don't use a regex buddy! PHP has a better suited tool for this...

$dom = new DOMDocument;

$dom->loadHTML($str);

$matchedAnchors = array();

$anchors = $dom->getElementsByTagName('a');

$match = 'http://link1';

foreach($anchors as $anchor) {

   if ($anchor->hasAttribute('href') AND substr($anchor->getAttribute('href'), 0, strlen($match)) == $match) {
      $matchedAnchors[] = $anchor;
   }

}

Upvotes: 0

Michael Robinson
Michael Robinson

Reputation: 29508

You're better off using something like simplehtmldom to find all links, then find all links with the relevant HTML / href.

Parsing HTML with regex isn't always the best solution, and in your case I feel it will bring you only pain.

$href = 'some_href';
$inner_text = 'some text';

$desired_anchors = array();

$html = file_get_html ('your_file_or_url');

// Find all anchors, returns a array of element objects
foreach($html->find('a') as $anchor) {
    if ($a->href = $href && $anchor->innertext == $inner_text) {
        $desired_anchors[] = $anchor;
    }
}

print_r($desired_anchors);

That should get you started.

Upvotes: 3

Related Questions