content copy problems

Discussion in 'PHP' started by alarik, Oct 26, 2006.

  1. #1
    I have a few problems with this script and it`s about 48 hours i`m stuck in it and can`t find an answer on any forums or help files,so I ask you for some help.Here is the code

    <?php
    $dbname='test';
    $con = mysql_connect("localhost","root","vertrigo");
    mysql_select_db($dbname) or die ( "unable to select database ".$dbname);
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    $pattern='Full name: </strong>([^<]+)<br>
    					<strong>Date of birth: </strong>([^<]+)<br>
    	 		<strong>Birthplace: </strong>([^<]+)<br>
    			<strong>Nationality: </strong>([^<]+)<br>';
    $dir='a';
    	if (is_dir($dir)) {
    	if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) { 
    	if ( $file !='.' && $file !='..' ) {
                $handle=fopen($file,"r");
    	echo "am deschis fisieru<br />";
    	$continut=fread($handle,filesize($file));
    	fclose($handle);	
                preg_match_all($pattern,$continut,$matches,PREG_PATTERN_ORDER);
    		        $query="INSERT INTO players (name,nationality) VALUES ($matches[0][0],$matches[1][0])";		   
    		$result=mysql_query($query); 					
    }
     	} 
         	closedir($dh); }
    	}
    	else echo "nu e director";
    ?>
    Code (markup):
    This script is suposed to read a directory 'a' and copy a certain content(pattern) from each file and insert it into my database.I`m having a lot of problems with it.
    Thank you for your time..
     
    alarik, Oct 26, 2006 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm going to assume the problem is with getting your pattern to match rather than opening the file or storing values in the database once/if you get a value.

    I can see a few problems with your pattern

    1. You need a limiting character at the beginning and end of the pattern - the forward slash is often used.
    2. You need to use a modifier (in this case x) if you want to use whitespace in the pattern
    3. You may also need the m (multiple lines) or s (single line) modifiers to get things to work - I can't recall which way around it is.
     
    streety, Oct 26, 2006 IP
  3. alarik

    alarik Active Member

    Messages:
    382
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    hm,thanks a lot for your answer.I got the script working,by the way.I apreciate your time
     
    alarik, Oct 28, 2006 IP