Jump to content

Parsing A Text File, Finding A Label


JustinK101

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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.