Jump to content

Tags Regex


azuka

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/26275-tags-regex/
Share on other sites

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>.
Link to comment
https://forums.phpfreaks.com/topic/26275-tags-regex/#findComment-121523
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.