Page 1 of 1

File Read

Posted: Wed Mar 17, 2010 10:24 am
by zemerick
My goal here is to read a txt file that has html tags in it (<TD>) then i'm going to insert whatever is between them into a database. Now the function works fine but only returns the first line in the file if you don't use a while loop, but I can't figure out why it will not work with the while loop using feof(). It just returns nothing.

Code: Select all

 
<?php
function get_string_between($string, $start, $end){ 
    $string = " ".$string; 
    $ini = strpos($string,$start); 
    if ($ini == 0) return ""; 
    $ini += strlen($start); 
    $len = strpos($string,$end,$ini) - $ini; 
    return substr($string,$ini,$len); 
} 
 
$file = "test.txt";
$fh = fopen($file, "r") or die("Can't open file");
 
while(!feof($fh)) {
 
$parsed = get_string_between($data, "<TD>", "</TD>"); 
echo $parsed."<br />";
 
}
fclose($fh);
 
?>
 
 

Re: File Read

Posted: Wed Mar 17, 2010 2:35 pm
by Christopher
You need to call fgets() or fread() inside the loop to read from the file into $data. You also might want to look a the preg_*() functions to extract the sub-string.

Re: File Read

Posted: Wed Mar 17, 2010 2:52 pm
by zemerick

Code: Select all

 
<?php
ini_set ('error_reporting',E_ALL);
ini_set ('display_errors','On');
include '/admin/config.php';
 
 
function get_string_between($string, $start, $end){ 
    $string = " ".$string; 
    $ini = strpos($string,$start); 
    if ($ini == 0) return ""; 
    $ini += strlen($start); 
    $len = strpos($string,$end,$ini) - $ini; 
    return substr($string,$ini,$len); 
} 
$file = "test.txt";
$fh = fopen($file, "r") or die("Can't open file");
while(!feof($fh)) {
$data = fgets($fh);
echo $data;
//$parsed = get_string_between($data, "<TD>", "</TD>");
$sql=mysql_query("INSERT INTO `scholarships` VALUES ('', '$data','','')") or die(mysql_error());
} echo "Success";
fclose($fh);
mysql_close($conn);
?>
Now I have this and when I insert it into the database it gets all junked up. It I can't find a solution.

Re: File Read

Posted: Wed Mar 17, 2010 7:16 pm
by Christopher
What does "it gets all junked up" mean?

Re: File Read

Posted: Mon Mar 22, 2010 1:30 pm
by zemerick

Code: Select all

 
<TD>Nossi College of Art Mahtaban Art Scholarship/Goodlettsville,TN (Students planning a career in commercial art through study at Nossi need to see information flyer in Career & College Center. For further information visit http://www.unidial.com/~nossi.)   </TD>
<TD>5 awards last year$1,800 renewable  </TD>
<TD>9/1/2008</TD>
 
That is an example of the input. I want each <td></td> into its own column in the row of the database. What happens is the first <td></td> will be in the first column or maybe the last. I can't figure out why it's gets out of order in the first place.

Re: File Read

Posted: Mon Mar 22, 2010 3:34 pm
by Christopher
zemerick wrote:I can't figure out why it's gets out of order in the first place.
Huh? Now your are inserting the values into a database? What is the schema for the table?

FYI - database table data is not in any "order" ... it is sortable.

Re: File Read

Posted: Tue Mar 30, 2010 11:35 pm
by zemerick
Yes, I stated that in the first post.

The schema is 4 columns id, text, amnt, date. So everytime it runs it should fill the 3 columns with the 3 <td>s that I'm working with.

http://www.livingstonwildcats.com/TEST/test.txt

The URL is my input file.