How to read and view docx Files using PHP |
X

Congrats, You are Subscribed to Receive Updates.

How to read and view docx Files using PHP


How to read and view docx Files using PHP. Now days processing Word Document is becoming more popular. Even you can create a new Word Document and process with it. My previous article describes you to create Word Document by using PHP.

Today we are going to discuss about reading the Docx files and convert it into text and view it online. Let’s begin with steps and codes,

<?php
function kv_read_word($input_file){	
	 $kv_strip_texts = ''; 
         $kv_texts = ''; 	
	if(!$input_file || !file_exists($input_file)) return false;
		
	$zip = zip_open($input_file);
		
	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;

		$kv_texts .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
			
		zip_entry_close($zip_entry);
	}
	
	zip_close($zip);
		

	$kv_texts = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $kv_texts);
	$kv_texts = str_replace('</w:r></w:p>', "\r\n", $kv_texts);
	$kv_strip_texts = nl2br(strip_tags($kv_texts,’‘));

	return $kv_strip_texts;
}
?>

The above function will helps you to get parse the text’s in a Word Document and  return it.

Now, we need to give the input file and its path as input to the function and print it for results.

<?php
$kv_texts = kv_read_word('path/to/the/file/kvcodes.docx');
if($kv_texts !== false) {		
	echo nl2br($kv_texts);	
}
else {
	echo 'Can't Read that file.';
}
?>

That’s it to read a docx file  and print it as text.

I have another article for WordPress user, who can try this to process Docx files using php and WordPress

How to Read and get Texts from Docx Files in WordPress

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

18 comments

  1. commenter

    Hi Did check your codes but cant make it word getting error on line 17 Undefined variable $kv_texts

  2. commenter

    Its really helps me .. but I can’t parse image form docs file ..

  3. commenter

    Change this one line of code to escape the apostrophe in the “Can’t” word:

    echo ‘Can\’t Read that file.’;

    Also, you might want to convert the ‘/r/n’ to line breaks, if you need to show the text as HTML

    $kv_strip_texts = nl2br(strip_tags($kv_texts,’‘));

    Otherwise, great little function that appears to work well with simple Word docs.

    …Rick…

  4. commenter

    hello brother may i know how to read multiple docx in php.. i already read with only one docx, but i need 2 file docx can be read thank’s i’ll visit you soon thank’s

  5. commenter

    Hi i am using the same code.Its working on the some docx ..But not docx..
    You know what when in the docx file have 2 tab space then function zip_entry_read() combined the word..I have alots of docx..I tested too much..BUt not found any exect solution..If you have any idea then please let me know

  6. commenter
    budi naini mindyarto

    Hi, I’ve run the code but it didn’t work out superscript/subscript, and lower case Greek symbols (α,β,γ,δ,ε,…).
    Please could you tell me how to do it?
    Thanks

  7. commenter

    HI,

    I am not able to read header content from the DOC file .Please help on that

  8. commenter

    If docx file contains an image within it then it is not showing after reading the docx file.

    Please help me.

Reply to Varadharaj V Cancel reply

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar