I'm doing a project now, and I'm stuck with reading word documents.
Word File content.
This is a test word file in PHP.
Thank you.
PHP code.
$myFile = "wordfile.docx";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 1000);
fclose($fh);
echo $theData;
output:
PK!éQ°Â[Content_Types].xml ¢( ´”MOÂ@†ï&þ‡f¯¦]ð`Œ¡pP<*‰Ïëv
«Ýì,_ÿÞiI¡(ziÒNß÷}fÚÞ`©‹h•5)ë&‘6Sf’²×ñc|Ë"Âd¢°R¶dƒþåEo
¼r€© ¦l‚»ãå´ÀÄ:0TÉ×"Пp'䧘¿îtn¸´&€ q(=X¿÷¹˜!.éñ
š„ä,º_¿WF¥L8W()ò²Êu <"œ›l.Þ%¤¬Ìqª^Nøp0ÙKPºl*Õ3Ó
«¢‘ðáIhbçë3žY9ÓÔwr¼¹F›çJB/Ýœ·é;é"©+Z(³e?ÈaUþ=ÅÚ÷Ä
ø7¦Ã<I?Hû<4ÆeÓÉ:bGÛž!ÐN ùþÛÆmCÇs+ÂÞ_þbǼ$§ó4ïœ
0ñ£¶n…´#€W×îٕͱH:#oÒÎñ¿h{»JuLGÎ êõÐtÄêDZXg÷åFÌ kÈæÕîÿÿPK
!ÇÂ'¼ß_rel
IS there anyway to read the word document in PHP ?
They can be read by using fopen. While . docx files are just zip files and xml files xml files in a zipfile container (source wikipedia) you can read them by using zip_open.
Just append your src attribute with an appropriate URL to a specific doc viewer, it will download your file from URL and then generate an HTML page from it, and then you direct your iframe to it and voila!
For docx use this function
function read_docx($filename){
$striped_content = '';
$content = '';
if(!$filename || !file_exists($filename)) return false;
$zip = zip_open($filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
zip_close($zip);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$striped_content = strip_tags($content);
return $striped_content;
}
It will return text from docx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With