KingOfHeart Posted April 13, 2011 Share Posted April 13, 2011 I need to search through a text file and output certain text. My current script doesn't work well at all. <? $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. Quote Link to comment https://forums.phpfreaks.com/topic/233555-read-data-from-text-file/ Share on other sites More sharing options...
KingOfHeart Posted April 13, 2011 Author Share Posted April 13, 2011 I hate these time limits where I can't modify my post. I wanted to explain it more. I used to have a wiki file with the name, function, and description for each coding command. I now need to transfer that data to a mysql field. So I need to search through this data, ignore the HTML, and just get the data from the name, function, and description. After I receive that data, instead of echoing it I'll just insert it into a mysql table(already know how to do this) The name of the function starts after <a name=. The function itself starts after <table style. Lastly the description starts after <p>. That's why I used strpos but probably doesn't work in this case. Quote Link to comment https://forums.phpfreaks.com/topic/233555-read-data-from-text-file/#findComment-1200928 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.