Created
April 23, 2012 20:00
-
-
Save salathe/2473432 to your computer and use it in GitHub Desktop.
AlVal
This file contains hidden or 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 | |
| $ul = '<ul id="menu"> | |
| <li id="Thing 1" class="some_class">Thing 1</li> | |
| <li id="Thing 2" class="someother_class">Thing 2</li> | |
| <li id="Thing 3" class="some_class">Thing 3</li> | |
| <li id="Thing 4" class="some_class">Thing 4</li> | |
| </ul>'; | |
| $dom = new DOMDocument; | |
| $dom->loadHTML($ul); | |
| $xpath = new DOMXPath($dom); | |
| // Finds all items that don't have class="some_class" *and* the | |
| // items that have class="some_class" except for the first two of them. | |
| $nodes = $xpath->query('//ul/li[@class != "some_class"] | (//ul/li[@class = "some_class"])[position() > 2]'); | |
| foreach ($nodes as $node) { | |
| $node->parentNode->removeChild($node); | |
| } | |
| $menu = $xpath->query('//ul[@id="menu"]')->item(0); | |
| echo $dom->saveXML($menu); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment