More information about the Underscore mailing list

[_] php regex - remove nested lists in nav

juan kennaugh contact at jkennaugh.co.uk
Fri Feb 1 11:23:31 GMT 2013

cheers for the info you 2 - I'm just using css at the moment - just want 
to see what was possible - could do it pretty simply with jquery

interesting dom parsing stuff though

thanks

Juan


On 01/02/2013 11:15, Martin Wheatley wrote:
> On 01/02/2013 11:04 AM, Martin Wheatley wrote:
>> On 01/02/2013 10:38 AM, juan kennaugh wrote:
>>> Hi all
>>>
>>> I'm no regex or php expert - so wondering if anyone could tell me if
>>> there's a simple way of removing a nested list from a nav markup string
>>> using regex?
>>>
>>> Cheers
>>>
>>> Juan
>>>
>>
>>
>> $doc = new DOMDocument();
>> $doc->loadHTML($string);
>>
>> $xpath = new DOMXPath($doc);
>>
>> foreach ($xpath->query('/html/body/ul/li/ul') as $node) {
>>      $node->parentNode->removeChild($node);
>> }
>>
>> $string = preg_replace(array("/^\<\!DOCTYPE.*?<html><body>/si",
>> "!</body></html>$!si"), "", $doc->saveHTML());
>>
>> (The last line strips off body etc that DOMDocumentadds, personally I
>> use SmartDOMDocument which is just a wrapper, to handle this rather than
>> DOMDocumen).
>>
>> Martin
>>
>
> Also, if it was me, I wouldn't put them in the string in the first 
> place if possible, but if I couldn't and the lists weren't large then 
> maybe just display: none, finally if you go the dom route, cache the 
> results so you don't need to keep doing it.
>
> Martin
>