如何在php中获取docx的首页内容


how to get first page content of docx in php

我只想获得docx文件的第一页文本,假设business.docx文件有5页,现在我只想显示第一页内容,隐藏其余4页文本。以下是读取docx文件的代码。

function read_file_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);
    }// end while
    zip_close($zip);
    //echo $content;
    //echo "<hr>";
    //file_put_contents('1.xml', $content);     
    $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;
}

我还想要ms word文本格式,如粗体、斜体、项目符号等…我有谷歌,但无法获得代码,这让我可以选择显示docx首页内容。

我建议您查看下面的php库

https://github.com/PHPOffice/PHPWord

你可以找到样品https://github.com/PHPOffice/PHPWord/tree/master/samples