Reading xlsx file in PHP - php

I'm following this tutorial to read the xlsx file format. I'm reading xlsx file. Working fine. But it display all the file content in one line. How to add space between them? Thanks
Here is my code.
$file_upload = 'book.zip';
$zip = new ZipArchive;
// the string variable that will hold the file content
$file_content = " ";
// the uploaded file
//$file_upload = $file -> upload["tmp_name"];
if ($zip -> open($file_upload) === true) {
// loop through all slide#.xml files
if ( ($index = $zip -> locateName("xl/sharedStrings.xml")) !== false ) {
$data = $zip -> getFromIndex($index);
$xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
$file_content = strip_tags($xml -> saveXML());
}
echo $file_content;
}

Solved. Just add this line.
$xml->formatOutput = true; Full code here.
$file_upload = 'book.zip';
$zip = new ZipArchive;
// the string variable that will hold the file content
$file_content = " ";
// the uploaded file
//$file_upload = $file -> upload["tmp_name"];
if ($zip -> open($file_upload) === true) {
// loop through all slide#.xml files
if ( ($index = $zip -> locateName("xl/sharedStrings.xml")) !== false ) {
$data = $zip -> getFromIndex($index);
$xml->formatOutput = true;
$xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
$file_content = strip_tags($xml -> saveXML());
}
echo $file_content;

Try this? Tested on PHP 5.5.3
$file_upload = 'book.zip';
$zip = new ZipArchive;
$dom = new DOMDocument;
// the string variable that will hold the file content
$file_content = " ";
// the uploaded file
//$file_upload = $file -> upload["tmp_name"];
if ($zip->open($file_upload) === true) {
// loop through all slide#.xml files
$index = $zip->locateName("xl/sharedStrings.xml");
if ($index !== false) {
$data = $zip->getFromIndex($index);
$dom->loadXML(
$data,
LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING
);
$dom->formatOutput = true;
$file_content = strip_tags($dom->saveXML());
}
}
echo $file_content;

Related

ZipArchive::addFromString() expects parameter 2 to be string, resource

I am making a page that will serve to decrypt .txt files that I will get from another website. I have been able to decrypt but only 1 to 1 so I am trying to decrypt 1 to 1 and put them all in a winrar. Unfortunately I'm having an error (post title) related to the "addFromString" method.
<?php
$zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CREATE);
$directory = 'uploadFiles/';
foreach (glob("uploadFiles/*.txt") as $file) {
$decrypted = decrypt_file($file,'pass');
$zip->addFromString($file, $decrypted);
}
if($res !== TRUE){
echo $res;
}
$zip->close();
function decrypt_file($file,$passphrase){
$iv = substr(md5("\x18\x3C\x58".$passphrase,true),0,8);
$key = substr(md5("\x2D\xFC\xD8".$passphrase,true).md5("\x2D\xFC\xD8".$passphrase,true),0,24);
$opts = array('iv'=>$iv, 'key'=>$key);
$fp = fopen($file,'rb');
stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
return $fp;
}
?>

CSV: Undefined Offset -> Convert to XML

I have this CSV file: http://www.gamesdeal.com/media/feedgenerator/Gamekey.csv
And get this error with PHP:
PHP Notice: Undefined offset: 6 in
But the problem is that I can not create the CSV file by my self. It is from a store. So, I can't modify it... Does somebody knows how I can fix this error?
Here my code:
function csvToXML($inputFilename, $outputFilename, $delimiter = ','){
// Open csv to read
$inputFile = fopen($inputFilename, 'rt');
// Get the headers of the file
$headers = fgetcsv($inputFile, 0, $delimiter);
// Create a new dom document with pretty formatting
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
// Add a root node to the document
$root = $doc->createElement('products');
$root = $doc->appendChild($root);
while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
$container = $doc->createElement('product');
foreach ($headers as $i => $header) {
$child = $doc->createElement($header);
$child = $container->appendChild($child);
$value = $doc->createTextNode($row[$i]);
$value = $child->appendChild($value);
}
$root->appendChild($container);
}
$strxml = $doc->saveXML();
$handle = fopen($outputFilename, 'w');
fwrite($handle, $strxml);
fclose($handle);
}
Here is the problem:
header: products_price <tab> price_currency
data: 5.45 EUR (no tab between 5.45 and EUR)
So in the header there are 7 fields defined, but only 6 in the data (also most records don't have a EAN value, but there's a tab at the end, so that should be ok).
To fix this you could:
read all the fields manually
first replace products_price <tab> price_currency with products_price price_currency in the header
remove price_currency from $headers
or somehow let the parser know there are only 6 fields instead of 7
You probably have to correct the price field afterwards then.

xml generated not opening in browser

I wrote php code to create an xml but the xml file is not opening in browser
The following is the code:
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('journal');
$dom->appendChild($root);
$journal_metadata = $dom->createElement('journal_metadata');
$dom->appendChild($journal_metadata);
$journal_metadata->appendChild($dom->createElement('full_title', 'Economics'));
$journal_metadata->appendChild($dom->createElement('abbrev_title', 'JJD'));
$issn = $dom->createElement('issn','2142');
$journal_metadata->appendChild($issn);
$issn->setAttribute('media_type', 'electronic');
$journal_metadata->appendChild($dom->createElement('doi_data', 'JJD'));
$doi_data = $dom->createElement('doi_data');
$journal_metadata->appendChild($doi_data);
$doi = $dom->createElement('doi', '10.E');
$doi_data->appendChild($doi);
$resource = $dom->createElement('resource', 'http://localhost/fo/journal.php?jid=1');
$doi_data->appendChild($resource);
// journal issue
$journal_issue = $dom->createElement('journal_issue');
$dom->appendChild($journal_issue);
// journal article
$journal_article = $dom->createElement('journal_article');
$dom->appendChild($journal_article);
$journal_article->setAttribute('publication_type', 'full_text');
$pages = $dom->createElement('pages');
$journal_article->appendChild($pages);
$resource = $dom->createElement('resource', htmlspecialchars('http://localhost/fo/abstracts.php?artID=472&jid=1'));
$doi_data->appendChild($resource);
echo '<xmp>'. $dom->saveXML() .'</xmp>';
$dom->save('result.xml') or die('XML Create Error');

xml generation from php if file doesnt exist

This code works if file is previously expisting but if file doesn't exist this code doesnt work.
$doc = new DOMDocument();
$doc->version = '1.0';
$doc->encoding = 'ISO-8859-1';
$response = $doc->createElement('response');
$doc->appendChild($response);
$response_type= $doc->createElement('response_type','Yes');
$response_id = $doc->createElement('response_id',$max_id_site);
$response->appendChild($response_type);
$response->appendChild($response_id);
$doc->formatOutput = true;
echo $doc->saveXML();
$doc->save('$filename_xml');
updated code
$doc = new DOMDocument();
$doc->version = '1.0';
$doc->encoding = 'ISO-8859-1';
$response = $doc->createElement('response');
$doc->appendChild($response);
$response_type= $doc->createElement('response_type','Yes');
$response_id = $doc->createElement('response_id',$max_id_site);
$response->appendChild($response_type);
$response->appendChild($response_id);
$doc->formatOutput = true;
echo $doc->saveXML();
if (! is_file($filename_xml)) {
touch($filename_xml) or trigger_error("Can't Create File");
$doc->save($filename_xml);
}
You can replace
$doc->save('$filename_xml');
with
if (! is_file($filename_xml)) {
touch($filename_xml) or trigger_error("Can't Create File");
$doc->save($filename_xml);
}
Use file_exists() to check if file is already there.
Replace last line:
$doc->save('$filename_xml');
with
if( file_exists( $filename_xml ) == false ) {
$doc->save( $filename_xml );
}
In general you should not even generate the xml if file is there.
BTW: Putting $filename_xml) in "`" is wrong.

Find linebreaks in a docx file using PHP

My PHP script successfully reads all text from a .docx file, but I cannot figure out where the line breaks should be so it makes the text bunched up and hard to read (one huge paragraph). I have manually gone over all of the XML files to try and figure it out but I cannot figure it out.
Here are the functions I use to retrieve the file data and return the plain text.
public function read($FilePath)
{
// Save name of the file
parent::SetDocName($FilePath);
$Data = $this->docx2text($FilePath);
$Data = str_replace("<", "<", $Data);
$Data = str_replace(">", ">", $Data);
$Breaks = array("\r\n", "\n", "\r");
$Data = str_replace($Breaks, '<br />', $Data);
$this->Content = $Data;
}
function docx2text($filename) {
return $this->readZippedXML($filename, "word/document.xml");
}
function readZippedXML($archiveFile, $dataFile)
{
// Create new ZIP archive
$zip = new ZipArchive;
// Open received archive file
if (true === $zip->open($archiveFile))
{
// If done, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false)
{
// If found, read it to the string
$data = $zip->getFromIndex($index);
// Close archive file
$zip->close();
// Load XML from a string
// Skip errors and warnings
$xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
$xmldata = $xml->saveXML();
//$xmldata = str_replace("</w:t>", "\r\n", $xmldata);
// Return data without XML formatting tags
return strip_tags($xmldata);
}
$zip->close();
}
// In case of failure return empty string
return "";
}
It is actually quite a simple answer. All you need to do is add this line in readZippedXML():
$xmldata = str_replace("</w:p>", "\r\n", $xmldata);
This is because </w:p> is what word uses to mark the end of a paragraph. E.g.
<w:p>This is a paragraph.</w:p>
<w:p>And a second one.</w:p>
Actually, why don't you use OpenXML? I think it works with PHP too. And then you don't have to go down to the nitty gritty file xml details.
Here is a link:
http://openxmldeveloper.org/articles/4606.aspx

Categories