sasori Posted January 19, 2010 Share Posted January 19, 2010 hi, I have a problem can you sir(s) tell me how to extract data from a messy text file and put it in mysql ? basically the sample data is something like this .;Stock Update as of ;.;TUE JUL 21, 2009 12:11:00 PM;ABA ;0.97 0.00 %;Abacus A ;50,000 ;ABS ;22.00 1.1494 %;ABS-CBN ;60,700 ;ABSP ;22.25 1.1364 %;ABS-CBN PDR ;78,500 ;AC ;280.00 -1.7544 %;Ayala Corp ;517,760 ;ACPR ;105.00 0.00 %;Ayala Pref. B ;5,920 ;AEV ;6.70 1.5152 %;Aboitiz ;278,000 ;AGI ;3.75 -2.5974 %;Alliance Global ;10,685,000 ;AJO ;0.0525 5.00 %;Ajo.Net ;100,000 ;ALCO ;0.15 0.00 %;Alco ;30,000 ;ALHI ;6.40 -1.5385 %;Anchor Land ;2,000 ;ALI ;8.90 -1.1111 %;Ayala Land ;9,422,000 ;AMC ;4.70 -6.00 %;Alaska Milk ;266,000 ;ANS ;2.34 0.00 %;Anscor ;239,000 ;AP ;5.80 -1.6949 %;Aboitiz Power ;7,544,000 ;APC ;0.36 2.8571 %;APC Group ;5,430,000 ;APO ;1.16 -1.6949 %;Anglo-Phil Hldg ;53,000 ;APX ;2.85 -3.3898 %;Apex Mining A ;5,000 ;APXB ;2.90 -1.6949 %;Apex Mining B ;47,000 ;AR ;0.0046 0.00 %;Abra Mining ; how am I gonna input that inside a db if its that jumbled. first thing i did was to try the readfile function $handle = "textfile.txt"; $handle = readfile($handle); echo $handle; but then the whole jumble words were just printed on the browser, how am i output the data something like this ? example data: ABA ;0.97 0.00 %;Abacus A ;50,000 ; ABA = Company Code 0.97 = Price 0.00% = Percent_change Abacus A = Company Name 50,000 = Volume Link to comment https://forums.phpfreaks.com/topic/188975-extracting-data-and-save-to-mysql/ Share on other sites More sharing options...
o3d Posted January 19, 2010 Share Posted January 19, 2010 $var_array_parent = explode("%", $InputString); var_dump($var_array_parent); //then foreach of those elements do this foreach($var_array_parent as $Value) { $var_array = explode(";", $Value); var_dump($var_array); } you could use this method if the data will always stay in that sequence. Modified: you can replace the var_dump inside the foreach section with your db statements. Link to comment https://forums.phpfreaks.com/topic/188975-extracting-data-and-save-to-mysql/#findComment-997808 Share on other sites More sharing options...
sasori Posted January 19, 2010 Author Share Posted January 19, 2010 sir there's something wrong, the sample data output should be example data: ABA ;0.97 0.00 %;Abacus A ;50,000 ; ABA = Company Code 0.97 = Price 0.00% = Percent_change Abacus A = Company Name 50,000 = Volume so a company has - company code - price - percent_change - company name - volume I altered your code like this $handle = file_get_contents("textfile.txt"); $var_array_parent = explode("%",$handle); foreach($var_array_parent as $value){ $var_array = explode(";",$value); echo "<pre>",print_r($var_array),"</pre>"; } and the output seem separating the "name" of the company from each of the company right after the percent because of the explode function ? Array ( [0] => . [1] => Stock Update as of [2] => . [3] => TUE JUL 21, 2009 12:11:00 PM [4] => ABA [5] => 0.97 0.00 ) 1 Array ( [0] => [1] => Abacus A [2] => 50,000 [3] => ABS [4] => 22.00 1.1494 ) 1 Array ( [0] => [1] => ABS-CBN [2] => 60,700 [3] => ABSP [4] => 22.25 1.1364 ) 1 Array ( [0] => [1] => ABS-CBN PDR [2] => 78,500 [3] => AC [4] => 280.00 -1.7544 ) 1 Link to comment https://forums.phpfreaks.com/topic/188975-extracting-data-and-save-to-mysql/#findComment-997812 Share on other sites More sharing options...
sasori Posted January 19, 2010 Author Share Posted January 19, 2010 i have fixed the code slightly to reg-group them $var_array_parent = explode("\n",$handle); foreach($var_array_parent as $value){ $var_array = explode(";",$value); $a = array_chunk($var_array,4); echo "<pre>",print_r($a),"</pre>"; } output Array ( [0] => Array ( [0] => ABA [1] => 0.97 0.00 % [2] => Abacus A [3] => 50,000 ) [1] => Array ( [0] => ABS [1] => 22.00 1.1494 % [2] => ABS-CBN [3] => 60,700 ) [2] => Array ( [0] => ABSP [1] => 22.25 1.1364 % [2] => ABS-CBN PDR [3] => 78,500 ) [3] => Array ( [0] => AC [1] => 280.00 -1.7544 % [2] => Ayala Corp [3] => 517,760 ) one last problem is that the price and percentage are sticking at each other on each of the company with key == 1 can you help me sir(s) how to solve this one last thing ? Link to comment https://forums.phpfreaks.com/topic/188975-extracting-data-and-save-to-mysql/#findComment-997820 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.