Help with PHP and reading text in to page from file

Discussion in 'Web Design and Development' started by edesignuk, Oct 27, 2009.

  1. edesignuk Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #1
    I can just about struggle though with PHP (and I have been), but now I'm stuck.

    Here's what I'd like to do...

    I have this in a text file on the web server local drive. Say C:\Logs\Expire.txt:
    Code:
    "cn=surname1\, firstname1 (company),ou=users,dc=domain,dc=net"
    "cn=surname2\, firstname2 (company),ou=users,dc=domain,dc=net"
    "cn=surname3\, firstname3 (company),ou=users,dc=domain,dc=net"
    
    I'd like to read this file from a web page using PHP, and display the file like this:
    Code:
    surname1, firstname1
    surname2, firstname2
    surname3, firstname3
    So I need to manipulate the file a little, trim out some stuff, but I have no idea how.

    Any ideas?

    Thank you!
     
  2. edesignuk thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #3
    Thank you.

    I'm sure between those I can do what I want, but if you could, would you mind explaining how exactly? (the code).
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Big-Ass-Warning: I don't use PHP at all. I am tackling this like a Perl programmer!

    I'd use preg_match_all

    Something like:

    Code:
    preg_match_all("cn=(.*)\\, (.*).*",$line,$matches);
    
    Assuming $line contains a single line as below then $matches should be a two element array with the first element containing the surname, the second element the first name.
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #5
    @OP:

    Tested with your data, works:

    PHP:
    // Read the file from disk and put contents into $data - adjust path and filename...
    $filename $_SERVER['DOCUMENT_ROOT']."/test.txt";
    $handle fopen($filename"r");
    $data fread($handlefilesize($filename));
    fclose($handle);

    // Create an array holding each line (determined by newlines terminator)..
    $lines_arr=explode("\n",$data);

    // Process each line of data...
    foreach ($lines_arr as $line) {
        
    // Correct regular expression, extract matches into $matches array for this line...
        
    preg_match_all("/cn=(.*?)\\\, (.*?) \(.*?/i",$line,$matches,PREG_SET_ORDER);
        
    // Display final output walking through $matches array for this line...
        
    foreach ($matches as $parsed) {
            print 
    "$parsed[1]$parsed[2]<br />";
        }
    }
    Instead of making a string holding your data, I created a disk file and made sure a newlines (carriage return) separated each line.

    I used both explode and preg_match_all (adjusting the expression posted by the other user, this one works) to demonstrate how each works in PHP and added comments to explain each segment of code. Obviously use your own filename and path and add in necessary code to check if the data file exists and if matches is null, etc., as would be done in production code.

    -jim
     
  5. edesignuk thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England

Share This Page