Skip to content Skip to sidebar Skip to footer

Convert A (nested)html Unordered List Of Links To Php Array Of Links

I have a regular, nested HTML unordered list of links, and I'd like to scrape it with PHP and convert it to an array. The original list looks something like this:

    Solution 1:

    Use DOMDocument and SimpleXMLElement along the lines of:

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xmlStr = $doc->saveXml($doc->documentElement);
    
    $xml = new SimpleXmlElement($xmlStr);
    
    $links = array();
    
    foreach ($xml->xpath('//a') as$li) {
        $links[] = $li->attributes()->href;
    }
    

    If href is being added to $links as a SimpleXMLElement, use ob_start and ob_clean to capture the string.

    Cheat sheet for xpath queries (pdf)

Post a Comment for "Convert A (nested)html Unordered List Of Links To Php Array Of Links"