1

I've exported my Firefox bookmarks as html so I can download my extensive music collection onto my phone, my problem is there is no easy way that I know of.

My intentions is to use PHP to parse the html into an array of the URLs

Heres what the html looks like

<DT><A HREF="https://www.youtube.com/watch?v=Ue8PpA557Bc" ADD_DATE="1477165404" LAST_MODIFIED="1477165404" ICON_URI="https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png" ICON="data:image/png;base64,">Don Diablo - Knight Time (Official Music Video) - YouTube</A>

How would I do this?

Adam
  • 143
  • 3
  • 18
  • Whoops sorry I was trying to use a block quote. – Adam Nov 01 '16 at 00:31
  • In that case I think either of the answers below would work for you. You also can see other options on the below thread. – chris85 Nov 01 '16 at 00:33
  • 1
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Nov 01 '16 at 00:33

2 Answers2

2

If you put in $html a correct html string, you could do it parsing the string with DOMDocument and selecting the href attributes with XPath.

<?php

$html = '<DT><A HREF="https://www.youtube.com/watch?v=Ue8PpA557Bc" ADD_DATE="1477165404" LAST_MODIFIED="1477165404" ICON_URI="https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png" ICON="data:image/png;base64,">Don Diablo - Knight Time (Official Music Video) - YouTube</A>';

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DomXPath($doc);

$nodeList = $xpath->query("//a/@href");

$links_array = [];

foreach($nodeList as $node){
  $links_array[] = $node->nodeValue;
}

echo "<pre>";
print_r($links_array);
echo "</pre>";

The output here is:

Array
(
    [0] => https://www.youtube.com/watch?v=Ue8PpA557Bc
)
nanocv
  • 2,227
  • 2
  • 14
  • 27
2
$doc = new DOMDocument();
$doc->loadHTML($bookmarks);
foreach ($doc->getElementsByTagName("a") as $node) { 
    $urls[] = $node->getAttribute("href");
}
Kayson
  • 425
  • 3
  • 15