php - Split one file and make them into two -


//expression found in file name $find = '.5010.';  //directory name //we store renamed files here $dirname = '5010'; if(!is_dir($dirname))     mkdir($dirname, 0777);  //read files directory //skip directories $directory_with_files = './'; $dh  = opendir($directory_with_files); $files = array(); while (false !== ($filename = readdir($dh))) {     if(in_array($filename, array('.', '..')) || is_dir($filename))         continue;      $files[] = $filename; }  //iterate collected files foreach($files $file) {     //check if file name matching $find     if(stripos($file, $find) !== false)     {          //open file         $handle = fopen($file, "r");         if ($handle)         {             //read file, line line             while (($line = fgets($handle)) !== false)             {                  //find ref line                 $refid = 'ref*2u*';                  if(stripos($line, $refid) !== false)                      {                      //glue refernce numbers                      //check if reference number not empty                       $refnumber = str_replace(array($refid, '~'), array('', ''), $line);                 if($refnumber != '')                      {                  $refnumber = '_'. $refnumber .'_';                   $filerenamed = str_replace($find, $refnumber, $file);                      copy($file, $dirname . '/' . $filerenamed);                          }                   echo $refnumber . "\n";                      }             }              //close file             fclose($handle);         }     } } 

i have code above, read files contain ".5010." , replace characters after ref*2u*. in files there more 1 ref*2u*, there code split them through line called "n1*pr*" , output 2 files each own ref*2u* character?

stream_get_line trick string stream_get_line ( resource $handle , int $length [, string $ending ] )

quoting documentation,

reading ends when length bytes have been read, when string specified ending found (which not included in return value), or on eof (whichever comes first).

so looping through lines of file checking fro specific string follows example:

$i = 1; while(! feof($file)) {     $contents = stream_get_line($file,1000,"ref*2u*");     file_put_contents('new_file_'.$i.'.txt',$contents);     $i++; } 

similar answer


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -