Jump to content


Photo

Parsing A Text File, Finding A Label


  • Please log in to reply
2 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 11 May 2006 - 07:08 AM

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:

//$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. */
}

Thanks for the help guys.

#2 Zubaz

Zubaz
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 07:16 AM

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

#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 11 May 2006 - 07:40 AM

Ok I am really close:

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

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 );
            }
        }
    }

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users