SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PCRE: html list (<ul>) to text

    Quick PCRE question...
    I want to replace list items contained within <ul></ul> w/ '* List Item'

    So:
    HTML Code:
    <ul>
    <li>List Item 1</li>
    <li>List Item 2</li>
    </ul>
    Becomes:
    Code:
    * List Item 1
    * List Item 2
    I can replace list items, with
    '/(?li>(.+?)</li>)/'
    but ordered lists need to be handled differently than unordered, so that won't work. To be honest, I haven't the slightest clue how to do this...

  2. #2
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Surprised I didn't think of this sooner...
    PHP Code:
    $str "<p>Some Text Here for some good fun mmhmmm</p>\n<ul>\n<li>List</li>\n<li>Item</li>\n</ul>\n\n<ol>\n<li>Listo</li>\n<li>Itemo</li>\n</ol>\n<ul>\n<li>List2</li>\n<li>Item2</li>\n</ul>\n";

    preg_match_all('!<ul>(.+?)</ul>!s'$str$matches);

    foreach (
    $matches[1] AS $string) {
        
    $var[] = preg_replace("/(?:<li>(.+?)<\/li>)/""* $1"$string);
    }
    // var_dump($matches[0]);
    // var_dump($var);

    $str str_replace($matches[0], $var$str);
    echo 
    $str


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •