Need help with php preg_split()

Discussion in 'PHP' started by diwebdesign uk, Aug 14, 2009.

  1. #1
    I have a string which contains a bunch of html. I want to be able to put different element contents into different arrays. For example I would like all of the contents within each <li></li> element to be put into an array.

    This is part of the code so far:
    $desc is the full string
    
    $listItems = preg_split("<li>",$desc, -1 , PREG_SPLIT_NO_EMPTY);
    
    Code (markup):
     
    diwebdesign uk, Aug 14, 2009 IP
  2. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    I believe preg_match_all would beter suit your needs.
     
    stOK, Aug 14, 2009 IP
  3. diwebdesign uk

    diwebdesign uk Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Maybe, but it is the reg expression that I need to use which I'm getting confused with.
     
    diwebdesign uk, Aug 14, 2009 IP
  4. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    The below regex should do what you want.

    $string = "<li>some stuff, and stuff</li>
    <li>some more stuff</li>";
    preg_match_all("/<li>(.*?)<\/li>/",$string,$stuff);
    print_r($stuff);

    It creates an array that looks like this:

    Array
    (
    [0] => Array
    (
    [0] => <li>some stuff, and stuff</li>
    [1] => <li>some more stuff</li>
    )

    [1] => Array
    (
    [0] => some stuff, and stuff
    [1] => some more stuff
    )

    )
     
    superdav42, Aug 14, 2009 IP
  5. diwebdesign uk

    diwebdesign uk Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks v much superdav42. Just what I needed.
     
    diwebdesign uk, Aug 18, 2009 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    szalinski, Aug 19, 2009 IP