Jump to content

Archived

This topic is now archived and is closed to further replies.

JustinK101

Parsing A Text File, Finding A Label

Recommended Posts

I have been pounding my head against the desk for hours. I can't figure out a solution.

I have a text file which is formatted accordingly:

label1 | 'value1'
label2 | 'value2'
label3 | 'value3'

NOTE: The labels and values are of variable length, so I can't just read in 6 chracters and assume that is a label.

I need to open the textfile, then search for a passed in label in the text file. If I find the lable then pull the value out and return the value stripping the single primes around the value.

If the label was not found simply return.

Here is what I have this far:

[code]
//$label is the label you are trying to find in $dir/$file
function getValueFromLabel($dir, $file, $label)
{
   $filePath = "language" . "/" . $dir . "/" . $file;
           
   if(!is_readable($filePath))
   {
      die("Error: Cannot read the language file: '" . $filePath . "'. Most likely the file does not exist.");
   }

   //I know the method is fopen, but the bbSoftware wont let me post that so I just underscored it.
   $filePtr = FO_P_E_N($filePath, "rt");

  /* I know I need to read until I encounter '|', then I need to strip the extra space out of the end of the string and see if that string is equal to $label. If it is, then return the value, if not, then I need to go to the next line and read in the label and continue this process until I reach end of file or I find the exact match of $label. */
}
[/code]

Thanks for the help guys.

Share this post


Link to post
Share on other sites
I think you would enjoy [a href=\"http://www.phpfreaks.com/phpmanual/page/function.preg-match.html\" target=\"_blank\"]preg_match[/a] and preg_match_all

Share this post


Link to post
Share on other sites
Ok I am really close:

NOTE: the function names fopen, fread, fgets need a underscore for me to post.

[code]
function getValueFromLabel($dir, $file, $label)
    {
        $filePath = "languages/" . $dir . "/" . $file;
        
        if(!is_readable($filePath))
        {
            die("Error: Cannot read the language file: '" . $filePath . "'. Most likely the file does not exist.");
        }
        
        $filePtr = f_open($filePath, 'r');
        
        //First Line In File
        if($curr_label = f_read($filePtr, strlen($label)) == $label)
        {
            preg_match("|*", $curr_label, $match);
            print_r( $match );
        }
        
        //The Rest Of The Lines In The File
        while($currentLine = f_gets($filePtr))
        {
            if($curr_label = f_read($filePtr, strlen($label)) == $label)
            {
                preg_match("|*", $curr_label, $match);
                print_r( $match );
            }
        }
    }
[/code]

The problem is the line preg_match. This doesnt work, I want it to search the current line for | and then return all characters in the line after the vertical bar. Thanks.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.