SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to open file and search for a string of text?

    Can anyone explain the process to open a file and then search to see if a given string of text is in that file? ie: Yes, the string is there.. or No, the string is not there.

    Thanks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do something like this:
    PHP Code:
    // Opens up a file for reading
    $filename '/home/mjacob/public_html/txt/lorem_ipsum.txt';

    /* 
    lorem_ipsum.txt
    ---------------
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr,  
    sed diam nonumy eirmod tempor invidunt ut labore et dolore 
    magna aliquyam erat, sed diam voluptua. At vero eos et 
    accusam et justo duo dolores et ea rebum. Stet clita kasd 
    gubergren, no sea takimata sanctus est Lorem ipsum...
    */

    $fd fopen($filename'r');
    $contents fread($fdfilesize($filename));
    fclose($fd);

    // The regexp for pattern-checking
    $pattern '/lorem/i';
    $results preg_match($pattern$contents);

    // Print out contents of $results
    var_dump($results); 
    Anyone else have a better method they'd like to share with the group?
    Last edited by mattjacob; Sep 5, 2002 at 23:08.


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
  •