thank you in advance for any assistance...

i have 100's of .txt files. Each file contains some data that i would like to put into a database. an example of a txt file would be something like this (smith.txt):

//

Forward Jean Smith


Current Team: Hawks 
First Year Played: 1990
Last Drafted: 09/02/2004
Up for Draft again: 2006

Background Information
Gender: Male
Family: Wife: Lynda Smith
2 Children: Kyle, Brett.
Birth date: 12/12/1970
Birthplace: New Haven, CT
Home City: Ft. Lauderdale, FL
Religion: Methodist

Education:
MBA, Yale University, 1995
BA, Yale University, 1992.

i would want to separate some of the useful data into variables for entry into a mysql database. I am assuming i would use the file() function. I guess my question is once i do that, what is the best way to set some variables from that array, for instance i would want data such as:
$current_team = "hawks"
$gender = "male"
$religion = "methodist"
$education = "MBA, Yale University, 1995 \n BA, History, Yale University, 1992."
notice the \n (newline) break in the last variable

    Well, using your txt file as a template:

    <?php
    $breaks = Array("\n", "\r\n", "\r", "<br>", "<br />");
    $contents = file_get_contents('smith.txt');
    $lines = explode("\r\n", $contents);
    
    $pattern = "/([a-zA-Z0-9.\.\/\,\s]*):\s([a-zA-Z0-9.\.\/\,\s]*)/";
    $i = 0;
    foreach($lines as $line)
    {
    	if($line != '' || !empty($line))
    	{
    		if($line == 'Education:')
    		{
    			$education = $lines[$i+1]."\n".$lines[$i+2];
    		}
    		@preg_match($pattern, $line, $info);
    		@$info[1] = strtolower(@str_replace(" ", "_", $info[1]));
    		if(preg_match("/([0-9_]*)children*/", $info[1]))
    		{
    			preg_match("/([0-9_]*)[a-z]*/", $info[1], $cnt);
    			$offset = strlen($cnt[1]);
    			$info[1] = substr($info[1], $offset);
    		}
    		${@$info[1]} = @$info[2];
    	}
    	$i++;
    }
    
    $dbc = mysql_connect('localhost', 'username', 'password');
    mysql_select_db('db_name', $dbc);
    
    $sql = "INSERT INTO `table` (Team, FirstYearPlayed, Drafted, DraftAgain, Gender, Family, Children, Birthdate, Birthplace, HomeCity, Religion, Education) VALUES ('$current_team', '$first_year_played', '$last_drafted', '$up_for_draft_again', '$gender', '$family', $children, $birth_date, $birthplace, $home_city, $religion, $education)"; 
    
    $result = mysql_query($sql);
    if(!$result)
    {
    	echo 'Query Failed!!';
    }
    else
    {
    	echo 'Query Successful!!';
    }
    
    ?>

    That's what I came up with....

    ~Brett

      Thats GREAT bpat1434! thank you.
      i had to remove the \r in the explode function but otherwise worked as expected. There are a few .txt files with other info in there but i think i can figure it out from here, if not i will post 😉

      wish me luck

      Thanks
      c

        Write a Reply...