-1

I want to find and print all mp3 links from a html web page.

<a href="http://example1.com">Test 1</a>
<a href="http://example2.com/page.html">Test 2</a>
<a href="http://example3.com/link/file2.mp3">mp3 link 2</a>
<a href="http://example3.com/link/file3.mp3">mp3 link 3</a>

I have that code of HTML now using php i want to print both mp3 links in my web page so please help me. i think DOM may help me here but i do not know how ? you may use DOM or other. Thanks you

Let clear -

$dom = new DomDocument();
$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $urls, $matches, PREG_SET_ORDER))
{ foreach($urls as $match)
{// $match[2] = link address
// $match[3] = link text}
}

That will print all link but i want to print a link which have .mp3 expedition. I think now it's clear. so please help

Rinku Yadav
  • 61
  • 1
  • 11
  • Also note that i am using 2 mp3 links here. But i will fetch entire html of a web page so i will not have info about How much mp3 links their. so please guide me to detect all mp3 links form a web page. that may be helpful to find [link](http://stackoverflow.com/questions/4624848/regexp-for-extracting-all-links-and-anchor-texts-from-html) – Rinku Yadav Dec 29 '13 at 04:39

2 Answers2

3

Do like this...

<?php
$html='<a href="http://example1.com">Test 1</a>
<a href="http://example2.com/page.html">Test 2</a>
<a href="http://example3.com/link/file2.mp3">mp3 link 2</a>
<a href="http://example3.com/link/file3.mp3">mp3 link 3</a>';
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $tag) {
        if(strpos($tag->getAttribute('href'),'.mp3')!==false)
        {
            echo $tag->getAttribute('href')."<br>";
        }

}

OUTPUT :

http://example3.com/link/file2.mp3
http://example3.com/link/file3.mp3
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

Using CSS3, you could select such elements with a selector like

a[href$=mp3]

Something similar in jQuery is what you're looking for.

I've created a Fiddle here: http://jsfiddle.net/myTerminal/Q4D9n/ The variable contains an array of links you are looking for.

myTerminal
  • 1,596
  • 1
  • 14
  • 31
  • In the fiddle, the below code alerts all the urls: `$.each(themp3Links, function(index, item){ alert($(item).attr("href")); });` – myTerminal Dec 29 '13 at 04:45
  • sorry friend you are not understanding my think. please check again my question. i do not want to apply css to them. ok – Rinku Yadav Dec 29 '13 at 05:04
  • If you follow the fiddle, you'll see that the script in there can find all the mp3 links on that page. I later read that you want to create something like a mini web crawler though. – myTerminal Dec 29 '13 at 14:58