how can i collect all the data between this 2 strings and put them in an array ?

Discussion in 'PHP' started by ramysarwat, Jul 15, 2010.

  1. #1
    how can i collect all the data between each <a> and </b> and put them in an array ?

    <a>1234</b>
    <a>5678</b>
    <a>abcd</b>
    <a>efgh</b>
    <a>ijkl</b>
    <a>mnop</b>
     
    ramysarwat, Jul 15, 2010 IP
  2. phpl0v3r

    phpl0v3r Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if you have the list in a file,
     
    phpl0v3r, Jul 15, 2010 IP
  3. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
        $data = '<a>1234</b>
                 <a>5678</b>
                 <a>abcd</b>
                 <a>efgh</b>
                 <a>ijkl</b>
                 <a>mnop</b>';
    
        preg_match_all("|<[ba]>(.*)</[ba]>|Ui", $data, $matches);
        $array = $matches[0];
        
        // $array now contains all the elements content
    ?>
    
    PHP:
     
    Deacalion, Jul 15, 2010 IP
  4. raredaredevil

    raredaredevil Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    ^^ I wrote a huge function to do this that used complex math to calculate string lenghts , extracted the start and end positions of the string and returned a array. Sometimes I make things harder for myself it seems although it was handy for learning string manipulation , great bit of code :).
     
    raredaredevil, Jul 15, 2010 IP
  5. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    lol, I know what you mean! I've over engineered things time and time again :)
     
    Deacalion, Jul 15, 2010 IP