PHP - Reading Specific Data From A Text File?
Hello All,
Please excuse my lack of knowledge in advance, I'm a beginner but pick things up relatively quickly, and tend to do things by trial and error. I would appreciate any help that anyone could offer with the following: I would like to retrieve two pieces of data from a "text file" and use them to calculate a result from a function I have in javascript. An example of the javascript code is: <script type="text/javascript"> function calculate() { var drybulb = document.calc_form.drybulb.value; var relhum = document.calc_form.relhum.value; var answer = ''; if (drybulb !== '' && relhum !== '') { answer = (0.567*drybulb*1) + (0.393 * (relhum*1/100 * 6.105 * Math.exp(17.27 * drybulb / (237.7 + drybulb*1)))) + 3.94; } document.calc_form.answer.value = answer; return false; } </script> Currently this is evaluated with entry from a simple html form, but I would like to have this performed "automatically". The "text file" that I would like to access is here, not on my server: http://www.bom.gov.au/fwo/IDV60901/IDV60901.94870.axf I would like to extract the "air_temp" and "rel_hum" values under the [data] section for use in my function. I just need the "most recent" values (i.e. first), not all those contained in the file. This is a single record from that section (sorry for the mess this no doubt makes): [data] sort_order,wmo,name[80],history_product[80],local_date_time[80],local_date_time_full[80],aifstime_utc[80],air_temp,apparent_t,cloud[80],cloud_base_m,cloud_oktas,cloud_type[80],cloud_type_id,delta_t,dewpt,gust_kmh,gust_kt,lat,lon,press,press_msl,press_qnh,press_tend[80],rain_trace[80],rel_hum,sea_state[80],swell_dir_worded[80],swell_height,swell_period,vis_km[80],weather[80],wind_dir[80],wind_spd_kmh,wind_spd_kt 0,94870,"Moorabbin Airport","IDV60901","27/10:00pm","20120227220000","20120227110000",18.8,19.3,"Partly cloudy",510,3,"-",-9999,0.8,17.5,13,7,-38.0,145.1,1013.5,-9999.0,1013.5,"-","11.4",92,"-","-",-9999.0,-9999,"10","-","S",11,6 Would it be possible to obtain these values using PHP?, and if so any advice on how this could be done (noting my minimal knowledge) would be greatly appreciated. Cheers. Similar TutorialsWell, im a n00b for php, and what i need is a php script which will read data from .xml file and write it into .txt file. Can someone help me? Example of .xml file : <?xml version="1.0" encoding="utf-8"?> <tv generator-info-name="NeTXMLTV/1.0.0.0" source-info-url="http://www.net-tv.hr/" source-info-name="NeT TV XMLTV" source-data-url="http://www.net-tv.hr/xmltv/net-pregled-programa-xmltv.xml"> <programme channel="NeT TV" start="20110413063000 +0200" stop="20110413080000 +0200"> <title lang="hr">JUTARNJI EXPRESS</title> <category lang="hr">glazbeno-informativni program</category> <desc lang="hr">Glazbeno informativna emisija sa servisnim informacijama i jutarnjim temperaturama, idealna za jutarnje buđenje uz toplu kavu ili čaj....</desc> </programme> </tv> I need the .txt file to look like this: 201104130630 - 201104130800 JUTARNJI EXPRESS glazbeno-informativni program Glazbeno informativna emisija sa servisnim informacijama i jutarnjim temperaturama, idealna za jutarnje buđenje uz toplu kavu ili čaj.... Can someone please help me? thanks hello everyone, please anyone can help me how to read data of excel file in php Hello again. I'm trying to load data from a Facebook Share Data XML file and then displaying them in a WordPress plugin. Here's my code so far: // Fetch Share Count Data function fb_button () { $fbshareUrl = urlencode(get_permalink($post->ID)); $fbshareTitle = urlencode($post->post_title); $fbLinkStats = simplexml_load_file('http://api.facebook.com/restserver.php?method=links.getStats&urls='.$shareUrl); $fb_params = '?u=' . $fbshareUrl . '&t=' . $fbshareTitle . '' ; // Vertical Facebook Button $fb_share = ' <div class="fsbsharer" id="fb"> <a href="http://www.facebook.com/sharer.php' . $fb_params . '" class="facebookShare"> <span class="fb_share_size_Small fb_share_count_wrapper"> <span></span> <span class="fb_share_count_nub_top "></span> <span class="fb_share_count fb_share_count_top"> <div class="fb_share_count_inner"> '. $fbLinkStats->link_stat->total_count .' </div> </div> <span style="cursor:pointer;" class="FBConnectButton FBConnectButton_Small"><span class="FBConnectButton_Text">Share</span></span> </a> </div> '; echo $fb_share; } This code works for everything except the title and the share count. I can fix the title myself but the share count isn't working. When I use this in plain HTML: <?php echo $fbLinkStats->link_stat->total_count.''; ?> it works fine, but when I combine it all into another function to echo, it no longer works properly. Thanks. Hello, I am trying to read data from a text file but also adding up the 4th column for example 'name' 'more' '1.00' 'evenmore' so were the 1.00 is I want to add it up is there anyway ? $tracking_file_location = "../dispatch-manager/logs/tracking_file.txt"; $tracking_file = fopen("$tracking_file_location", "r") or die("Unable to open file!"); $tracking_file_size = filesize("$tracking_file_location"); $tracking_file_text = fread($tracking_file,$tracking_file_size); fclose($tracking_file); I need to read all the rows from a text file between two specific lines into an array where I can process them. I then need to do the same thing again until the end of the file. The file is too large to read the entire file into an array. The number of rows that are between the key lines are variable. This is what I have so far to read the first set of rows into the array but it is only reading the first row when in fact in this case there are 15. Any pointers as to where I am going wrong please? if ($_POST['srch']=="Import") { //open file for reading $family_rec=[]; $file_handle = fopen($fn, "r"); if(!$file_handle){echo "Could not open file";} while(!feof($file_handle)) { $line = fgets($file_handle); if ((strpos($line, "0 @") !== false) && (strpos($line, "FAM") !== false)) { $famref = explode("@", $line); $line = fgets($file_handle); //echo $line."<br>"; do{ $family_rec[]=$line; $line = fgets($file_handle); //echo $line."<br>"; } while((strpos($line, "0 @") !== false) && (strpos($line, "FAM") !== false)); print_r($family_rec); }//end of family }//end of while not eof }//end of import
My user needs me to update the database on their server from data contained in a text file but do not want me to upload the text file to the server but rather read the file from wherever it exists. Across the intranet. Uploading the file means that whomsoever needs to do this update requires write permission to a folder on the server & my user does not want that. Suggestions on how I should solve this problem would be greatly appreciated. BTW My code works fine if I upload the file and do the update in place on the server. Hi, I have this text file with a 123mb size then i ran my simple script to read it and render it via browser $fh = fopen('worldcitiespop.txt',r); while(!feof($fh)) { $content = fgets($fh); echo $content."<br />"; } fclose($fh); I was expecting the 'whole' output via browser but then it didn't show "all" because when I opened the text file via a wordpad, it's really huge.. can you tell me what's wrong with my script ?...( btw, there's no max execution time error at all ) Okay I don't mind telling you I am very perplexed on how to read a text file delimited with TAB into a HTML Table using PHP. I don't know where to begin, I have read about 12 examples and get more confused each time. I hope someone can help me. I am looking to read this delimited (TAB) file skipping the first line and reading array's 2,5,6,7,8,9, and 10, then displaying in a HTML Table. Corresponding Headers would be nice. The php file does not work at the present because I am trying to read the array sections by row.
Thank You in advance
Attached Files
ParseText.php 852bytes
3 downloads
JimTest.txt 16.88KB
1 downloads I am trying to print out rows of images and pieces them to form a bigger map but having a little trouble printing out correctly. I am doing the following... A numbered text file with rows of numbers like these: 33.txt Code: [Select] 48,48,49,49,48,47,48,49,48,50,50,48,50,49,48,49,49,49 49,11,4,8,50,11,4,4,8,48,11,4,4,8,48,11,8,50 ... The number represent different area and the number in the text file represent each individual pieces of that particular area. the number of rows and columns are arbitrary. Example: Here is my code at the moment but there is something wrong in it as I can't seems to correctly print everything out... Code: [Select] <?php $file = file_get_contents("33.txt"); // can be any numbered text files $entries = explode("\n", $file); echo "Entries <br /><hr />"; for($i=0;$i < sizeof($entries);$i++){ for($j=0;$j < sizeof($entries);$j++){ $data = explode(",", $entries[$j]); echo "<td align=\"center\" background=\"test/33_"; echo $data[$j]; echo ".gif\" width=\"65\" height=\"65\"> <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"65\" height=\"65\"> <tbody><tr> <td> <center></center> </td> </tr></tbody> </table> </td>"; } } ?> So, I'm trying to get this read my text document "news.txt" and display the contents. My code: Code: [Select] div.newscontent{ padding:0px; margin-top:60px; margin-bottom:0px; margin-left:400px; margin-right:0px; position:absolute; width:457px; height:330px; text-align:center; color:#FFFFFF; } Code: [Select] <div class='newscontent'> <?PHP $filename = "news.txt"; $arr = file($filename); foreach($arr as $line){ print $line . "<br/>"; } ?> </div> This is the output I get: Code: [Select] "; } ?> Whats wrong? I'm testing out small bits of code for myself, and I'm trying to understand why this isn't coming out how I would imagine it to... I have my function which stores colors in an array from a text file, then I return back a random color from my array like so... function getColor() { $ball_color = explode("\n", file_get_contents('colors.txt')); // Takes file contents and stores them into an array. return $ball_color[array_rand($ball_color)]; } And now in the main program, I simply have this: $color = getColor(); switch($color) { Case "blue": // echo stuff break; Case "red": // echo stuff break; etc... } For some reason though, my switch statement always reads the default case, making me assume that the words "blue" "red" "green" etc... aren't matching up to what I have set the cases at. When I output what color is actually being read, it displays exactly as it should, so I don't understand why the case "red" isn't opening up when that color is chosen, or same with any of the other colors. Is there some small conflict I'm unaware of in this tiny practice code? Any help is appreciated, thanks. The title says it all,..well as all u can see, there are many methods to accomplish this done but not before undergoing through a heavy tasking process. For instance, 1. Open the file 2. Reads the content of the file into array. 3. Find out the desire line of text that we want to replace. 4. Make that line of text as variable and assigns with the new data(that we want to replace) as its values. 5.open another file. 6.Write the whole contents to the file opened in 5. 7. Put the contents back to the file of original. Can anyone contributes a better way to get this done? Like replace/delete a specific line without affecting other characters or anything therein? Thanx. P.S reading and using str_replace to make the alteration always ends up in giving some kind of result beyond desired after the first round of code execution. Is this possible? Could the following code be replaced with one .txt consisting of two digits? Code: [Select] $myFile = "offerrotation.txt"; $myFile2 = "lprotation.txt"; $fh = @fopen($myFile, 'r'); $offerNumber = @fread($fh, 5); @fclose($fh); $fh = @fopen($myFile2, 'r'); $lpNumber = @fread($fh, 5); @fclose($fh); Many thanks is there a php solution that will take the raw data from one file and paste it into another text file? I have a master file and a download file that always starts at quarter past midnight . this data needs to be pasted into the master file, in the correct place , possibly overlapping and replacing data that already exists. the length of the download file will vary but will always start at 00. 15 the two files are www.maidenerleghweather.com/download.txt and also www.maidenerleghweather.com/master.txt any help appreciated . i have a text file that has spaces between each column. eg 12 12 13 22 34.5 10 13 18 88 32.5 12 33 17 23 22.3 (the actual data is weather data that accumulates a new line every 15 mins and is seen at www.maidenerleghweather.com/clientraw.txt ) each column represents a weather variable. i would like to use php to find the maximum values in each column and minimum values etc etc. how can i get the text data into arrays, so that i can then easily perform some simple stats on it? HOWEVER each line would NOT be an array. the first value in each line makes up array number one, second makes up two etc any ideas for a newbie beginner would be welcomed. can someone give me a push here? I've tried "file_put_contents" and "fputs" and neither one works for me. I'm running PHP on an IIS server and a windows hosting plan. PHP version is 7.3.1 I believe. here is my code: if ($validation = "true") { file_put_contents("ValidationsRedeemed.txt", $validationKey . "|", FILE_APPEND); //$handle = fopen("ValidationsRedeemed.txt", "a"); //fwrite($handle, $validationKey . "|"); //fclose ($handle); } else { //user entered a product key that is not recognized. tell them to try again. echo "<script>alert('The product key entered is not valid.'+'\\n\\n'+'Please try again.'); window.location.href='test.php';</script>"; } thanks! I need to search through a text file and output certain text. My current script doesn't work well at all. Code: [Select] <? $file = 'flist.txt'; $handle = @fopen($file, "r"); $names = array(); $func = array(); $desc = array(); $nme; $fnc; $dsc; $s = -1; $e = -1; $n = 0; if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { if($nme == 0) { if(strpos($buffer,"<a name") !== false) { $nme = 1; $s = strpos($buffer,"<a name"); } } else if($nme == 1) { if(strpos($buffer,"</span>") !== false) { $nme = 2; $e = strpos($buffer,"</span>"); $num = $e - $s; $names[$n] = strip_tags(substr($buffer,$s,$num)); } } else if($nme == 2) { if($fnc == 0) { if(strpos($buffer,"<table style") !== false) { $fnc = 1; $s = strpos($buffer,"<table style"); } } else if($fnc == 1) { if(strpos($buffer,"</td>") !== false) { $fnc = 2; $e = strpos($buffer,"</td>"); $num = $e - $s; $func[$n] = strip_tags(substr($buffer,$s,$num)); } } else if($fnc == 2) { if($dsc == 0) { if(strpos($buffer,"<p>") !== false) { $dsc = 1; $s = strpos($buffer,"<p>"); } } else if($dsc == 1) { if(strpos($buffer,"</p>") !== false) { $fnc = 0; $nme = 0; $dsc = 0; $e = strpos($buffer,"</p>"); $num = $e - $s; $desc[$n] = strip_tags(substr($buffer,$s,$num)); } } } } } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); $n = 0; while($n < 100) { echo $names[$n] . "<br>"; echo $func[$n] . "<br>"; echo $desc[$n] . "<p>"; $n ++; } } ?> 4096 gets the data for each line, right? Well some of my data uses two lines. After all the searching is done I need it to output $names, $func, and $desc in plain text. Hi Everyone, I'm new to PHP freaks, and I'm hoping someone might be able to help me. I have written some code for a html page and used php to retrieve confirm whether or not data is in a text file. I also tried to write some code to insert the data supplied to my html page to the text file but it's not working. Can someone help me figure out what my issue is. I have attached my text file, and my php code as well. Below you'll find the code I used for my html page. Thank you for all your help, Phee <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Telephone Directory</title> </head> <body> <form action='SignGuestBook.php' method='post'> <h1>Sign Guest Book</h1> <hr> <br> <table align='Left'> <tr> <td>Name: </td> <td><input name='name' /></td> </tr> <tr> <td>E-mail: </td> <td><input name='email' /></td> </tr> <tr> <td><input type="submit" value='Sign' /></td> <td><input type="reset" value='Reset Form' /></td> </tr> </table> <h3></h3> <br> <h4></h4> <br> <h5></h5> <br> <h6></h6> <br> <h7></h7> <br> <hr> <a href="http://helios.ite.gmu.edu/~smohamu2/IT207/Lab%20Assignment%208/AddNew.html">View Guest Book</a> </form> </body> </html> [attachment deleted by admin] |