Extract text from file

Discussion in 'PHP' started by moso, Mar 10, 2007.

  1. #1
    Hello,
    I want to know haw can I extract some text from a file using php.
    Example:

    sometext=string-725
    name="string" and nickname="string+as"

    Haw can I extract the value string (can be any value) from a text that has the same format, only the value of string is different, and can be found in an unknow number of times.
     
    moso, Mar 10, 2007 IP
  2. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    With a regular expression you could do this in about 4 lines of code... but...

    Can you be more clear about what the text file is supposed to look like EXACTLY, and what the script is supposed to take out of the file EXACTLY?
     
    Robert Plank, Mar 10, 2007 IP
  3. moso

    moso Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Let's say is a html file and I want to extract a value from a url inside that html file.
    We will have "http://www.mysite.com/index.php?articleid=3200"
    I want to take the value of articleid from that html file, every time an url with that structure is found in that html file.
     
    moso, Mar 10, 2007 IP
  4. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    u can use file_get_contents function, and then use regex to identify the <a> tag.

    I hope its useful cause its my first seed post in PHP.:d
     
    protocol96, Mar 10, 2007 IP
  5. moso

    moso Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I know haw to load the text into a variable, but I don't know what function to use and the syntax to find that url and extract articleid value.
     
    moso, Mar 10, 2007 IP
  6. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #6
    This is off the top of my head, and its late, so you might want to check for errors :)

    But you could try something like this...
    
    <?
    $string = 'Some text here, maybe we got it from some source code? Here is an example link: <a href="http://site.com?id=0">test</a>!';
    preg_match("/<a href=\".*\">.*<\/a>/", $string, $matches);
    
    
    print 'Our String: ' . $string;
    print '<br /><br />';
    print 'Match: ' . htmlspecialchars($matches[0]);
    ?>
    
    PHP:
    You're going to need to fix the regex (the first option in preg_match between the quotes) so that it works more efficiently. But that's the basic idea. I'll see if I can be of more help in the morning :)
     
    Louis11, Mar 11, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Regex could be more efficient, try this :

    
    <?
    preg_match_all("/articleid=([0-9]+)/si", $your_html_data, $article_ids );
    print_r($article_ids['1']);
    ?>
    
    PHP:
     
    krakjoe, Mar 11, 2007 IP
    moso likes this.
  8. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #8
    Ah yea, that looks a hell of a lot better :)
     
    Louis11, Mar 11, 2007 IP
  9. moso

    moso Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you. Problem solved.
     
    moso, Mar 11, 2007 IP