Home > Article > Backend Development > How to convert php rich text to html
How to convert php rich text to html: 1. Open the corresponding code file and modify the image path; 2. Use phpword to convert to html, with code such as "$phpWord = new \ PhpOffice \ PhpWord \ PhpWord... ”
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php How to convert rich text to html?
PHP rich text to html, word, pdf file download
Rich text I use layui here because it is light...
As for rich text, what about it? It is very simple to use the documentation. If you have any questions, please tell me: http://www.layui.com/doc/modules/layedit.html
Then after accessing the rich text data, take out the content you want:
1. Transfer to html
$html = "Here is the content you want!";
The first line is because of the picture The path is wrong and cannot be displayed. All the image paths must be replaced correctly
or you can use phpword to convert it to html. For the phpword demo above, you can refer to
phpword (composer): composer require phpoffice/phpword
<?PHP require_once ' bootstrap.php中' ; //创建新文档... $ phpWord = new \ PhpOffice \ PhpWord \ PhpWord(); / *注意:您附加到文档的任何元素都必须位于节中。* / //添加一个空的部分到文档中... $ section = $ phpWord - > addSection(); //添加文本元素默认......风格的字体有第 $节- > addText( “ ‘借鉴昨天,活在今天,憧憬明天。‘ ’最重要的是不要停止问问题。’ ”。'(爱因斯坦)'); / * *注意:可以通过三种方式自定义添加的Text元素的字体样式: * - inline; * - 使用指定的字体样式(隐式创建新的字体样式对象); * - 使用明确创建的字体样式对象。 * / //'添加带有字体自定义内联的文本元素... $ section - > addText( ''伟大的成就通常是由于伟大的牺牲''。'而且永远不是自私的结果。''。'(Napoleon Hill)', array( ' name ' => ' Tahoma ', ' size ' => 10)); //使用指定字体样式自定义字体添加文本元素... $ fontStyleName = ' oneUserDefinedStyle ' ; $ phpWord - > addFontStyle( $ fontStyleName, array( ' name ' => ' Tahoma ', ' size ' => 10, ' color ' => ' 1B2232 ', ' bold ' => true)); $ section - > “最大的成就是不是永远不跌倒,‘ ’但在再度上涨你掉下去了。” '。'(Vince Lombardi)',$ fontStyleName); //添加使用明确创建的字体样式对象自定义字体的文本元素... $ fontStyle = new \ PhpOffice \ PhpWord \ Style \ Font(); $ fontStyle - > setBold( true); $ fontStyle - > setName( ' Tahoma '); $ fontStyle - > setSize( 13); $ myTextElement = $节- > addText( ' “相信你能和你\'重新一半。“(西奥多·罗斯福)'); $ myTextElement - > setFontStyle($ fontStyle); //将文档保存为OOXML文件... $ objWriter = \ PhpOffice \ PhpWord \ IOFactory :: createWriter( $ phpWord, ' Word2007 '); $ objWriter - > save( ' helloWorld.docx '); //将文档保存为ODF文件... $ objWriter = \ PhpOffice \ PhpWord \ IOFactory :: createWriter( $ phpWord, ' ODText '); $ objWriter - > save( ' helloWorld.odt '); //将文档保存为HTML文件... $ objWriter = \ PhpOffice \ PhpWord \ IOFactory :: createWriter( $ phpWord, ' HTML '); $ objWriter - > save( ' helloWorld.html '); / *注意:我们跳过RTF,因为它不是基于XML的,需要一个不同的例子。* / / *注意:我们跳过PDF,因为“HTML-to-PDF”方法用于创建PDF文档。* /
2. Convert to word
(1) Use PHP’s built-in file_put_contents(). After I try to save it as word, all the original html tags will exist unless You need rich text or html source code, otherwise there is no point.
(2) Utilize cache: To display the image after downloading it locally, only the network address (IP or domain name) can be used
$wors = str_replace("/uploads/layui/","http://172.16.3.125/notes/public/uploads/layui/",$html); $this->start(); $newname = 'pppp'; $wordname = 'files/word/'.$newname.".doc";//生成文件路径 echo $wors; $this->save($wordname); ob_flush();//每次执行前刷新缓存 flush(); function start() { ob_start(); echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <xml><w:WordDocument><w:View>Print</w:View></xml> </head><body>'; } function save($path) { echo "</body></html>"; $data = ob_get_contents(); ob_end_clean(); $this->wirtefile ($path,$data); } function wirtefile ($fn,$data) { $fp=fopen($fn,"wb"); fwrite($fp,$data); fclose($fp); }
(3) Utilize phpword can also be converted to word. As shown in the phpword demo above, you can refer to it. However, after the image is downloaded to the local computer and displayed, only the network address (IP or domain name) can be used. For mht and then transfer the example of the picture text to word, I thought it was too long, so I will take a look at it later ε≡٩(๑>₃
3, transfer to pdf
( 1) HTML-to-PDF: I heard about this but I haven’t tried it. If you have used it, you can give me some advice
(2) Use mpdf (Mapo Tofu) extension (used to composer): composer require mpdf/ mpdf
public function topdf() { $html = Db::table('diary')->where('id',12)->find(); $title = '<h1 style="text-align: center;">' . $html['notename'] . '</h1>'; $content = $html['content']; $mpdf = new Mpdf(); $mpdf->autoScriptToLang = true; $mpdf->autoLangToFont = true; $header='<table width="95%" style="margin:0 auto;border-bottom: 1px solid #4F81BD; vertical-align: middle; font-family: serif; font-size: 9pt; color: #000088;"><tr> <td width="10%"></td> <td width="80%" style="font-size:16px;color:#A0A0A0">这是我的页眉</td> <td width="10%" style="text-align: right;"></td> </tr></table>'; $mpdf->SetHTMLHeader($header); //页眉 // $mpdf -> WriteHTML(' <h1> Hello world!</h1> '); $mpdf -> WriteHTML($title.$content); $mpdf->Output(); // $mpdf -> Output('files/word/one.pdf','D'); //存为文件 }
Welcome everyone to point out the incorrect parts/bow
Here is a friend who wrote more details https://blog.csdn.net/wepe12/article/details/52796348
It’s finally over... (Any advice welcome)
Recommended study: "
PHP Video TutorialThe above is the detailed content of How to convert php rich text to html. For more information, please follow other related articles on the PHP Chinese website!