Sanjoy Roy

[MCM, MCP, SCJP] – Senior PHP Programmer

Tag Archives: Parser

Parse .xls (Excel) using PHP


XLSParser.php:

if ($fileType !='' && $fileType == 'application/vnd.ms-excel'){
		//parse xsl file
		 $ext = 'xls';
		 $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		 copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);
		 require_once '../includes/reader.php';

		$data = new Spreadsheet_Excel_Reader();
		 //set output encoding
		$data->setOutputEncoding('CP1251');
		//read the excel file through api
		$data->read($UPLOAD_CSV_PATH.$filename);
		//prepare to retun result
		$actual_data = $data->sheets[0]['cells'];

		$rowsLength	= array();
		for($i=1; $i<$max_nos; $j++){
				$allColumns[$j] = $actual_data[$i][$j];
			}
			$newRowData[] =  $allColumns;
		}
		$data = $newRowData;
  }

Parse .CSV using PHP


CSVParser.php:

if ($fileType !='' && $fileType == 'text/comma-separated-values'){
		//parse csv file
		 $ext = 'csv';
		 $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		 copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);

		 $row 				= 1;
		$fhandle 			= @fopen($UPLOAD_CSV_PATH.$filename, "r");
		$columnHeaders		= '';
		$columnNames		= '';
		$rawData			= array();
		$count = 0;
		while (($fdata = fgetcsv($fhandle, 1000, $terminated_by, $enclosed_by)) !== FALSE) {
			$len = count($fdata)-1;
			if (strlen($fdata[$len])==0) array_pop($fdata);
			$rawData[$count] = $fdata;
			$count++;
		}
		fclose($fhandle);

		$newRowData = array();

		for($i=0; $iencode($rawData);

  }

Parse XML Spreadsheet using PHP DOMDocument


xmlparser.html:

File Name:  
Options: Column Heading Included
Column Heading & Name Included
Just Data
 
Hidden Parameters:

 

xmlparser.php:

$fileType 	= isset($_FILES['file']['type']) ? $_FILES['file']['type']: '';
if ($fileType !='' && $fileType == 'text/xml'){
	  $ext = 'xml';
	  if ($_FILES['file']['tmp_name']){
	  	  //Copy to the backup directory
	  	  $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		  copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);
		  //parse xml spredsheet
		  $dom 		= DOMDocument::load($UPLOAD_CSV_PATH.$filename);
		  $rows 	= $dom->getElementsByTagName('Row');
		  $data		= array();

		  foreach ($rows as $row){
			  $index 	= 1;
			  $cells 	= $row->getElementsByTagName('Cell');
			  $tempdata = array();
			  foreach( $cells as $cell ){
				  $ind = $cell->getAttribute('Index');
				  if ( $ind != null ) $index = $ind;
				  //echo $cell->nodeValue."
"; $tempdata[] = $cell->nodeValue; $index += 1; }//inner for loop $data[] = implode(",", $tempdata); }//outer for loop }//outer if (file temp_name compare) }