Need to pull some data out of a string

Discussion in 'PHP' started by Scoty, Feb 18, 2011.

  1. #1
    Ok so I have documents containing specific data I would like to sort into a database.
    The bit I'm now having trouble with is pulling the data from the string.

    For example:

    $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Donec sit amet: 7899942378
    Nunc ornare nulla in quam tempus sed";

    The number of lines before and after the data (in green) varies doc to doc, however "Donec sit amet: " before the data will always remain the same*, so I need to find the line containing "Donec sit amet: " then take what is left until the end of the line. EDIT: The length of the data also changes, e.g sometimes it's +1 or -1 the usual length.

    *Actually there is some variation, but not a lot so I was going to either change it manually when I need to, or throw in an if statement.

    I did try searching for this but couldn't come up with anything.

    Thanks,
    Scot
     
    Last edited: Feb 18, 2011
    Scoty, Feb 18, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    
    <?php
        $string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                    Donec sit amet: 7899942378
                    Nunc ornare nulla in quam tempus sed";
        
        preg_match("#Donec sit amet: (.*?)\r\n#i",$string,$matches);
        
        echo $matches[1];
    ?>
    
    PHP:
     
    tvoodoo, Feb 18, 2011 IP
    Scoty likes this.
  3. Scoty

    Scoty Active Member

    Messages:
    620
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks! :D
     
    Scoty, Feb 19, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Your welcome !
     
    tvoodoo, Feb 19, 2011 IP