JustinK101 Posted May 11, 2006 Share Posted May 11, 2006 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/$filefunction 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 https://forums.phpfreaks.com/topic/9530-parsing-a-text-file-finding-a-label/ Share on other sites More sharing options...
Zubaz Posted May 11, 2006 Share Posted May 11, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9530-parsing-a-text-file-finding-a-label/#findComment-35182 Share on other sites More sharing options...
JustinK101 Posted May 11, 2006 Author Share Posted May 11, 2006 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 https://forums.phpfreaks.com/topic/9530-parsing-a-text-file-finding-a-label/#findComment-35192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.