More information about the Underscore mailing list

[_] php regex - remove nested lists in nav

Steve Lacey steve at stevelacey.net
Sat Feb 2 19:53:32 GMT 2013

I think the main point to take away here is that unless you're pulling this
data from a third-party via some crazy HTML returning API or just plain
scraping. You really shouldn't be doing this.

Fix whatever is returning you the wrong HTML. If that's inappropriate,
DOMParser, if you can't be arsed, regex, if you can't get the regex to
work, tell the client it's a feature.


On 1 February 2013 11:23, juan kennaugh <contact at jkennaugh.co.uk> wrote:

> 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
>>
>>
>
> --
> underscore_ list info/archive -> http://www.under-score.org.uk/**
> mailman/listinfo/underscore<http://www.under-score.org.uk/mailman/listinfo/underscore>
>



-- 

Steve Laceyhttp://stevelacey.net@stevelacey<http://www.twitter.com/stevelacey>
 / steve at stevelacey.net