Last active
February 23, 2022 21:36
-
-
Save gingerbeardman/0008ba0eaf03050e1c1492ea57314d35 to your computer and use it in GitHub Desktop.
import browser bookmarks.html FAST using a regex with optional named groups
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
// href and title are required | |
// everything else is an optional named group, using format \s*(ATTR="(?P<attr>.*?)")? | |
$pattern = '|<DT><A HREF="(?P<href>.*?)"\s*(ADD_DATE="(?P<add_date>.*?)")?\s*(LAST_MODIFIED="(?P<last_modified>.*?)")?\s*(ICON_URI="(?P<icon_uri>.*?)")?\s*(ICON="(?P<icon>.*?)")?\s*(PRIVATE="(?P<private>.*?)")?\s*(TOREAD="(?P<toread>.*?)")?\s*(TAGS="(?P<tags>.*?)")?>(.*?)</A>|'; | |
$string = file_get_contents("bookmarks.html"); | |
preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER); // all pattern matches get their own sub-array | |
$array_filtered = array_filter($matches, "is_string", ARRAY_FILTER_USE_KEY); // keep only named arrays (our groups) | |
echo count($array_filtered[href]); // show count | |
// print_r($array_filtered); // print everything |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested with exported bookmarks HTML from:
Supports attributes: