File Read

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zemerick
Forum Newbie
Posts: 4
Joined: Wed Mar 17, 2010 10:19 am

File Read

Post 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);
 
?>
 
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File Read

Post 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.
(#10850)
zemerick
Forum Newbie
Posts: 4
Joined: Wed Mar 17, 2010 10:19 am

Re: File Read

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File Read

Post by Christopher »

What does "it gets all junked up" mean?
(#10850)
zemerick
Forum Newbie
Posts: 4
Joined: Wed Mar 17, 2010 10:19 am

Re: File Read

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File Read

Post 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.
(#10850)
zemerick
Forum Newbie
Posts: 4
Joined: Wed Mar 17, 2010 10:19 am

Re: File Read

Post 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.
Post Reply