2017-10-19 5 views
0

zip_read()を使用して.docxファイルを読み込んでいますが、.docxではページ区切りが<w:br w:type="page"></w:br>となっています。私はそれを<br style="page-break-before: always">に変えて、HTMLに出力することができます。どうやってやるの?ありがとう!PHPを使って.docxの改行をHTMLの改行に変換するには

+0

方法については 'str_replace'をhttps://github.com/PHPOffice/PHPWordをチェックするようにしてください? – rtfm

+0

は動作していないようです – user2335065

+1

あなたのコードでは推測できません。 – rtfm

答えて

0

あなたはDOMでそれを読むことができるので、これは、XMLファイル形式です:要求されたように、これは、HTMLページ区切りに単語改ページを変換します

$xml = <<< WORD 
<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<?mso-application progid="Word.Document"?> 
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"> 
    <w:body> 
    <w:br w:type="page"></w:br> 
    </w:body> 
</w:wordDocument> 
WORD; 

libxml_use_internal_errors(true); 
$dom = new DOMDocument(); 
$dom->loadXML($xml); 
libxml_clear_errors(); 

$xpath = new DOMXPath($dom); 
$xpath->registerNamespace('w', 'http://schemas.microsoft.com/office/word/2003/wordml'); 

// find all the page breaks 
foreach ($xpath->evaluate('//w:br[@w:type="page"]') as $page_break) { 
    // create an html break element with some style attribute 
    $html_break = $dom->createElement('br'); 
    $html_break->setAttribute('style', 'page-break-before: always'); 
    // replace the page break with the html break in the document 
    $page_break->parentNode->replaceChild($html_break, $page_break); 
} 
echo $dom->saveHTML(); 

<?mso-application progid="Word.Document"><w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"> 
    <w:body> 
    <br style="page-break-before: always"> 
    </w:body> 
</w:wordDocument> 

ないこと、それはxmlという言葉の残りの部分がそのままであることを考えると、かなり意味をなさないでしょう。しかし、それはXMLパーサーを使ってそれを処理する方法です。

確認が同様

関連する問題