w1ww Posted February 17, 2010 Share Posted February 17, 2010 I've a dynamically generated html list like this one: <ul> <li><a href="#">Item 1</a> </li> <li>Item 2<ul> <li>Item 2.1 <ul> <li><a href="#">Item 2.1.1</a> </li> <li><a href="#">Item 2.1.2</a> </li> <li><a href="#">Item 2.1.3</a> </li> <li><a href="#">Item 2.1.4</a> </li> <li><a href="#">Item 2.1.5</a> </li> </ul></li> <li><a href="#">Item 2.2</a> </li> <li><a href="#">Item 2.2.1</a> </li> <li><a href="#">Item 2.2.2</a> </li> <li><a href="#">Item 2.2.3</a> </li> <li><a href="#">Item 2.2.4</a> </li> <li><a href="#">Item 2.2.5</a> </li> </ul></li> <li>Item 3 <ul> <li><a href="#">Item 3.1</a> </li> <li><a href="#">Item 3.1</a> </li> </ul></li> <li>Item 4 <ul> <li><a href="#">Item 4.1</a> </li> <li><a href="#">Item 4.2</a> </li> <li>Item 4.3 <ul> <li><strong class="selflink">Item 4.3.1</strong> </li> <li><a href="#">Item 4.3.2</a> </li> </ul></li> </ul></li> <li><a href="#">Item 5</a> <ul> <li><a href="#">Item 5.1</a> </li> <li><a href="#">Item 5.2</a> </li> <li><a href="#">Item 5.3</a> </li> <li><a href="#">Item 5.4</a> </li> <li><a href="#">Item 5.5</a> </li> </ul></li> </ul> I need to add another "li" before the last "</ul>". How can I do it? Regex or ..? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/192335-add-string-before-closing-list/ Share on other sites More sharing options...
premiso Posted February 17, 2010 Share Posted February 17, 2010 If you "dynamically" generate it, why not add it where you generate it? Link to comment https://forums.phpfreaks.com/topic/192335-add-string-before-closing-list/#findComment-1013522 Share on other sites More sharing options...
w1ww Posted February 17, 2010 Author Share Posted February 17, 2010 It is generated by a CMS, I can't hack the core. I get the list in a variable. Link to comment https://forums.phpfreaks.com/topic/192335-add-string-before-closing-list/#findComment-1013525 Share on other sites More sharing options...
premiso Posted February 17, 2010 Share Posted February 17, 2010 $string = preg_replace('~</ul>$~s', "<li></li>\n</ul>", $string); Should do the trick for you. Link to comment https://forums.phpfreaks.com/topic/192335-add-string-before-closing-list/#findComment-1013542 Share on other sites More sharing options...
w1ww Posted February 17, 2010 Author Share Posted February 17, 2010 Yup! Thank you Link to comment https://forums.phpfreaks.com/topic/192335-add-string-before-closing-list/#findComment-1013570 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.