RadioTunes.in RadioTunes.in - 1 month ago 9
HTML Question

How to export specific A tags from a file.[html] in PHP

i have a html file and some A tags in it.

i need to export some specific A tags from it.

here is the whole html file HTML_FILE_LINK

i want to extract only

dl/l/Lucy.2014.Dubbed.Audio.TinyMoviez_co(Onyx).mp3?hash=27bd643109ad5f992c28e10a33afe8dc_159484_26806_3


and the other ones like this.

how can i do this in php?

i have tried this : how to get a list of links in a webpage in PHP? but didnt work.

i really need this if anyone could answer i would appreciate .

Answer

Building on this answer from Parse Website for URLs gives already all a tags

$code = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($code);
$links = array();
foreach ($doc->getElementsByTagName('a') as $element) {
    if ($element->hasAttribute('href')) {
        $links[] = $element->getAttribute('href');
    }
}

But you want only specific links, so you must check if it satisfies some condition. Extract all relevant parts and see if it matches

$text = $element->nodeValue;
$link = $element->getAttribute('href');
if ($text == "لینک مستقیم ویژه") {
    echo "$link\n";
}

Putting everything together gives

$code = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($code);
foreach ($doc->getElementsByTagName('a') as $element) {
    if ($element->hasAttribute('href')) {
        $text = $element->nodeValue;
        $link = $element->getAttribute('href');
        if ($text == "لینک مستقیم ویژه") {
            echo "$link\n";
        }
    }
}