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
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
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.