verdrm Posted July 14, 2010 Share Posted July 14, 2010 I have a text file and I need to find a specific line and output the contents of the line. Example (in file): CurrentVersion = 1.2.3.4 I need to get the "1.2.3.4" after CurrentVersion. How can I do that? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/ Share on other sites More sharing options...
bh Posted July 14, 2010 Share Posted July 14, 2010 The structure is like an ini file. Try to parse your file with http://php.net/manual/en/function.parse-ini-file.php Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085915 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 Thanks. It isn't an INI file though. How would I do this regardless of the file extension? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085919 Share on other sites More sharing options...
bh Posted July 14, 2010 Share Posted July 14, 2010 Theres a lot of choiches. Two of them: http://php.net/manual/en/function.file-get-contents.php http://php.net/manual/en/function.file-get-contents.php and google!!!: http://www.google.hu/search?sourceid=chrome&ie=UTF-8&q=php+file+read Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085921 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 Could you provide a code example for finding the line and outputting it? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085924 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 The parse_ini_file function doesn't care what the file extension is. Let's say I have a file named "x.x" which contains: line1 line 2 line 3 CurrentVersion = 1.2.3.4 Using <?php $x = parse_ini_file('x.x'); ?> $x contains Array ( [CurrentVersion] => 1.2.3.4 ) Ken Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085927 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 I tried that against my INF file using the code you provided. When I used a file with test data: [Config] one = two I could output "two" using $e[one]; However, in the actual file I am using, this does not work. [Version] signature = "$Windows NT$" Class = "ActivityMonitor" ClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} Provider = %Symc% DriverVer = 06/24/2009,12.8.0.10 CatalogFile = SymEvent.cat Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085934 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 You're getting errors because there are strings containing non alphanumeric characters that are not quoted. If you change your file to contain: [Version] signature = "$Windows NT$" Class = "ActivityMonitor" ClassGuid = "{b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2}" Provider = "%Symc%" DriverVer = "06/24/2009,12.8.0.10" CatalogFile = SymEvent.cat then the function will work. Ken Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085943 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 The file is that way on many machines, and can't be changed manually. How else can I do this without using parse_ini_file()? I just need a code example for using fread to find the line and display it. Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085957 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 I used parse_ini_file() on the data you posted and got the expected results - [signature] => $Windows NT$ [Class] => ActivityMonitor [ClassGuid] => {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} [Provider] => %Symc% [DriverVer] => 06/24/2009,12.8.0.10 [CatalogFile] => SymEvent.cat Perhaps if you defined: this does not work? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085958 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 Can you post the exact code you used? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085963 Share on other sites More sharing options...
Pikachu2000 Posted July 14, 2010 Share Posted July 14, 2010 I think you want just the contents of the one specific line, right? Easy enough to do; just foreach() loop through the result of parse_ini_file (which is $x) and echo the $key and $value where the $key == your_string. Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085965 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 The problem is that I can't get the results to print out. Code: $x = parse_ini_file('file.inf'); print_r($x); This doesn't work. My Data: [Version] signature = "$Windows NT$" Class = "ActivityMonitor" ClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} Provider = %Symc% DriverVer = 06/24/2009,12.8.0.10 CatalogFile = SymEvent.cat Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085972 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 My code - <?php $x = parse_ini_file('config.inf'); echo "<pre>",print_r($x,true),"</pre>"; ?> Are you sure the file is named like you are using in the code and it is not actually - file.inf.txt? Are you developing and debugging your php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the errors php detects would be reported and displayed? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085987 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 Yes, I checked all of those things you mentioned. Is it the illegal characters in the file? I can't remove them. Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085988 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 I'm getting this error using your snippet of code: Warning: parse error, expecting `TC_DOLLAR_CURLY' or `TC_QUOTED_STRING' or `'\"'' Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085991 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 I copy/pasted your exact posted values. Best guess is this is an actual Windows generated .inf file and it contains some non-printing characters. Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085993 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 This is interesting, when I use xampp (Windows 7, php 5.3.1), it works perfectly. When I use the PHP CLI on my Linux box (php v5.2.9), I get a error parsing the file. When I move the script & file that work under php 5.3.1 to the linux box and run it I get the error: Warning: Error parsing config.inf on line 4 in /home/rbnsn/public_html/phpfreaks/test_ini.php on line 2 the file contains [Version] signature = "$Windows NT$" Class = "ActivityMonitor" ClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} Provider = %Symc% DriverVer = 06/24/2009,12.8.0.10 CatalogFile = SymEvent.cat Ken Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1085997 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 I managed to get it to work using code on the PHP manual page for the ini parser. Array ( [Version] => Array ( [signature] => $Windows NT$ [Class] => ActivityMonitor [ClassGuid] => {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} [Provider] => %Symc% [DriverVer] => 06/24/2009,12.8.0.10 [CatalogFile] => SymEvent.cat ) The only issue now is how do I extract DriverVer? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1086003 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 Assuming the output of the function is stored in $out, you would do <?php echo $out['Version']['DriverVer']; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1086013 Share on other sites More sharing options...
Pikachu2000 Posted July 14, 2010 Share Posted July 14, 2010 Does the file exist in the same directory as the script? Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1086015 Share on other sites More sharing options...
verdrm Posted July 14, 2010 Author Share Posted July 14, 2010 Thanks. Problem solved. Quote Link to comment https://forums.phpfreaks.com/topic/207727-output-line-in-file/#findComment-1086020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.