azuka Posted November 6, 2006 Share Posted November 6, 2006 I'm nearly going crazy here, trying to get this stuff to run. Basically, I'm trying to capture the output from a script, use regex to transform it and so on. Here's what I have. I'm supposed to run through:[code]<li class="page_item"><a href="http://localhost/blog/about-me/" title="About Me">About Me</a><ul> <li class="page_item"><a href="http://localhost/blog/about-me/underling1/" title="Underling1">Underling1</a> </li> <li class="page_item"><a href="http://localhost/blog/about-me/underling2/" title="Underling2">Underling2</a> </li></ul></li><li class="page_item"><a href="http://localhost/blog/calendar/" title="Calendar">Calendar</a></li><li class="page_item"><a href="http://localhost/blog/contact/" title="Contact">Contact</a></li><li class="page_item"><a href="http://localhost/blog/links/" title="Links">Links</a></li>[/code]remove all the 'li's inside uls and integrate them, separating them by commas. Ie[list][*]About Me[/list] [list] [*]Underling1 [*]Underling2 [/list][list][*]Calendar[*]Contact[*]Links[/list]Should be changed to [list][*]About Me[/list] [list] [*]Underling1, Underling2 [/list][list][*]Calendar[*]Contact[*]Links[/list]I've tried using the simple regular expression"/\<ul\>(.*?)\<\/ul\>/e"but it simply does nothing. When I replace the /e with /is, replacements occur. unfortunately, what I need to do is apply a function to the captured text with preg_replace. SO gar, I haven't been successful.Does anyone have an idea?[/list] Quote Link to comment Share on other sites More sharing options...
rea|and Posted November 8, 2006 Share Posted November 8, 2006 Mmm, try to use preg_replace_callback (it works like preg_replace with "e" modifier on). Something like :[code=php:0]function cBack_one($matches){$tmp=preg_split('/\s*<(?:\/li|li[^>]*)>\s*/is',$matches[1],-1, PREG_SPLIT_NO_EMPTY) ;return '<li>'.implode( ', ', $tmp ).'</li>';}echo preg_replace_callback('/<ul>(.*?)<\/ul>/is','cBack_one',$your_text) ;[/code]It'd have to work with the text you posted above, where actually there is no nested <ul>. Quote Link to comment Share on other sites More sharing options...
azuka Posted November 18, 2006 Author Share Posted November 18, 2006 Thanks for the reply. I actually gave up on this topic and while browsing through the php.net function reference, discovered preg_replace_callback.Thanks all the same. Quote Link to comment 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.