SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array within an array?!?

    Hoping someone can tell me what I'm doing wrong here;

    Have a string which looks something like;

    PHP Code:
    <log-entry>1001377804|22851|5|DATA_WARNING|Tue Sep 25 00:30:04 2001|The  Calculation returned 1 for transaction id 1668993</log-entry>
    <
    log-entry>1001377804|22851|5|DATA_WARNING|Tue Sep 25 00:30:04 2001|The  Calculation returned 1 for transaction id 1668993</log-entry>
    <
    log-entry>1001377804|22851|5|DATA_WARNING|Tue Sep 25 00:30:04 2001|The  Calculation returned 1 for transaction id 1668993</log-entry>
    etc


    Every line is basically the same format and has markers so that it's real easy to break into arrays in PHP.

    Now here's what I'm doing to it ( the string is called $catfile )

    PHP Code:
    if ( ereg "<log-entry>"$catfile ) ) { 
       
    $catfile ereg_replace "</log-entry>"""$catfile ); // Remove the end of line tag           
    }

    $catfile explode "<log-entry>"$catfile ); // Break into an array using the start of line tag

    while (list ($key$line) = each ($catfile)) { // Now pull out each line one by one into $line

       
    $line explode "|"$line ); // And break up $line using the | character

       
    echo ( "<p>" $line[4] . $line[3] . $line[5] . "</p>"); //Display the desired elements of one line


    The problem is this only returns the first line of the string $catfile.

    The problem seems to lie with exploding $line because if I simply output $line of each element of $catfile, without breaking it up first, I get all the origional string back.

    Can someone tell me what the problem is here?

  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apologies - learnt a lesson in being more careful.

    The string actually contains tags like

    PHP Code:
    <log_entry> and <log_entry
    I'd been working with
    PHP Code:
    <log-entry> and <log-entry

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what happened with your code. But I know this works cause Ijust tested it.

    PHP Code:
    <?
        $fp 
    fopen("spf.txt""r");
        while(!
    feof($fp)) {
            
    $data .= fgets($fp,1024);
            }
        
    fclose($fp);
        
    preg_match_all("/(<[^>]*>)([^<]*)(<\/[^>]*>)/"$data$args);
        foreach(
    $args[2] as $key => $tmp) {
            
    $line explode("|"$tmp);
            echo ( 
    "<p>" $line[4] . $line[3] . $line[5] . "</p>"); 
            }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got the origional code working as well but many thanks for the tip Freddy - have updated the code to look like yours and it seems to output the results faster. Read somewhere the preg generally requires less processing than ereg and still getting to grips with arrays in PHP so it's great to see other techniques.

    As a matter of general interest, the thing I'm working on at the moment is a series of monitoring tools to watch the performance of an application running on Solaris - the code above is for browsing log files generated by the application. Other features so far include monitoring Daemons and watch the status (e.g. size) of a database (Oracle 8 at the moment).

    I'm trying to make it as general and flexible as possible, so in future, the code can be reused for monitoring anything running on Solaris or Linux as well as providing generally useful information about whats going on with the OS. There are a few tools out there for free already I know (although they tend to be specific to Linux), and I'm looking at trying to integrate this code with them in future.

    Once done I'll make it available somewhere, like Source Forge perhaps. If anyone is interested in working on this (or just checking I'm not making newbieish mistakes) drop me a line at hfuecks@atraxis.com - should have the first version available in about 1 month - current project at work has me tied up right now.


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
  •