Created
December 22, 2012 18:11
-
-
Save AmusableLemur/4360279 to your computer and use it in GitHub Desktop.
A very simple class to make extraction of bookmarks from Chrome and Firefox easier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A very simple class to make extraction of bookmarks from Chrome and Firefox | |
* easier. Bookmarks need to be exported from the browser in order to be read. | |
* | |
* Example: | |
* $bp = new BookmarkParser( | |
* array( | |
* "bookmarks_chrome.html", | |
* "bookmarks_firefox.html" | |
* ) | |
* ); | |
* | |
* var_dump($bp->getBookmarks()); | |
*/ | |
class BookmarkParser | |
{ | |
private $bookmarks; | |
private $source; | |
/** | |
* Loads all specified source files | |
* @param string[] $sources | |
*/ | |
public function __construct($sources) | |
{ | |
$this->source = array(); | |
if (!is_array($sources)) { | |
$sources = (array)$sources; | |
} | |
foreach ($sources as $source) { | |
$this->source = array_merge($this->source, file($source)); | |
} | |
} | |
/** | |
* Parses source files and extracts bookmark data | |
*/ | |
private function extractBookmarks() | |
{ | |
$this->bookmarks = array(); | |
foreach ($this->source as $line) { | |
// Makes sure the line is a bookmark | |
if (strpos($line, "<A") !== false) { | |
$bookmark = array(); | |
$line = trim($line); | |
$url = $this->extractString("HREF=\"", "\"", $line); | |
// To remove Firefox links | |
if (substr($url, 0, 6) === "place:") { | |
continue; | |
} | |
$this->bookmarks[$url] = array( | |
'url' => $url, | |
'date' => $this->extractString("DATE=\"", "\"", $line), | |
'icon' => $this->extractString("ICON=\"", "\"", $line), | |
'name' => $this->extractString("\">", "<", $line), | |
); | |
} | |
} | |
} | |
/** | |
* Extracts a substring from a string | |
* @param string $start | |
* @param string $end | |
* @param string $source | |
* @return string|null Returns null if delimiter wasn't found | |
*/ | |
private function extractString($start, $end, $source) | |
{ | |
$offset = strpos($source, $start); | |
if ($offset === false) { | |
return null; | |
} | |
$offset += strlen($start); | |
$end = strpos($source, $end, $offset); | |
$length = $end - $offset; | |
return substr($source, $offset, $length); | |
} | |
/** | |
* Fetches the parsed bookmarks | |
* @return string[][] | |
*/ | |
public function getBookmarks() | |
{ | |
if (!isset($this->bookmarks)) { | |
$this->extractBookmarks(); | |
} | |
return $this->bookmarks; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment