lfernando Posted April 11, 2014 Share Posted April 11, 2014 Hi there, I've been trying to get this to work but no dice, hope one of you can help me? I need to find all "<li>" tags that do not contain a certain string, and insert another string. For example, find "<li>" tags that do not contain "123" and replace them with "<li id="123">" // Pear, Watermelon and Tomato do not contain 123 <ul> <li id="123_1" title="" value="1">Banana</li> <li id="123_2" title="" value="1">Apple</li> <li>Pear</li> <li title="" value="1">Watermelon</li> <li id="456_2" title="" value="1">Tomato</li> </ul> <ul> <li id="123_1" title="" value="1">Banana</li> <li id="123_2" title="" value="1">Apple</li> <li id="123" >Pear</li> <li id="123" title="" value="1">Watermelon</li> <li id="123" title="" value="1">Tomato</li> </ul> Any help would be much appreciated! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/287704-does-not-contain-between-two-tags/ Share on other sites More sharing options...
requinix Posted April 11, 2014 Share Posted April 11, 2014 (edited) Another way of describing that is adding an id=123 to all LIs that don't already have an id=*123*. Use DOMDocument. $html = <<<HTML <ul> <li id="123_1" title="" value="1">Banana</li> <li id="123_2" title="" value="1">Apple</li> <li>Pear</li> <li title="" value="1">Watermelon</li> <li id="456_2" title="" value="1">Tomato</li> </ul> HTML; $dom = new DOMDocument(); $dom->loadHTML($html); foreach ($dom->getElementsByTagName("li") as $li) { if (!$li->hasAttribute("id") || !fnmatch("*123*", $li->getAttribute("id"))) { $li->setAttribute("id", "123"); } } $output = ""; foreach ($dom->getElementsByTagName("body")->item(0)->childNodes as $child) { $output .= $dom->saveHTML($child); } echo $output;The final $output stuff may vary depending on what your input HTML actually is. ... You do realize that it's not allowed to use the same ID for multiple elements, right? Edited April 11, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/287704-does-not-contain-between-two-tags/#findComment-1475816 Share on other sites More sharing options...
lfernando Posted April 14, 2014 Author Share Posted April 14, 2014 Thanks. Yes, I only put the same ID on the thread for example, but it's actually being set by a variable. So there is no way to do this using regex? ie, find a pattern that does not contain a string, replace with something else? Quote Link to comment https://forums.phpfreaks.com/topic/287704-does-not-contain-between-two-tags/#findComment-1476054 Share on other sites More sharing options...
Ch0cu3r Posted April 14, 2014 Share Posted April 14, 2014 requinix code already does this, specifically this part if (!$li->hasAttribute("id") || !fnmatch("*123*", $li->getAttribute("id"))) { $li->setAttribute("id", "123"); } Have you tried replacing the 123 in the lines above with your variable? Quote Link to comment https://forums.phpfreaks.com/topic/287704-does-not-contain-between-two-tags/#findComment-1476097 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.