Home > Backend Development > PHP Problem > How to convert php rich text to html

How to convert php rich text to html

藏色散人
Release: 2023-03-13 20:34:02
Original
2819 people have browsed it

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... ”

How to convert php rich text to html

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!";

How to convert php rich text to html

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  &#39; bootstrap.php中&#39; ;
//创建新文档... 
$ phpWord  =  new  \ PhpOffice \ PhpWord \ PhpWord();
/ *注意:您附加到文档的任何元素都必须位于节中。* /
//添加一个空的部分到文档中... 
$ section  =  $ phpWord - > addSection(); 
//添加文本元素默认......风格的字体有第
$节- > addText(
 “ ‘借鉴昨天,活在今天,憧憬明天。‘ ’最重要的是不要停止问问题。’ ”。&#39;(爱因斯坦)&#39;);    
         
         
/ *
 *注意:可以通过三种方式自定义添加的Text元素的字体样式:
*  -  inline; 
*  - 使用指定的字体样式(隐式创建新的字体样式对象); 
*  - 使用明确创建的字体样式对象。
* / 
//&#39;添加带有字体自定义内联的文本元素... 
$ section - > addText(
 &#39;&#39;伟大的成就通常是由于伟大的牺牲&#39;&#39;。&#39;而且永远不是自私的结果。&#39;&#39;。&#39;(Napoleon Hill)&#39;, array( &#39; name &#39; => &#39; Tahoma &#39;, &#39; size &#39; => 10));    
         
         
        
//使用指定字体样式自定义字体添加文本元素... 
$ fontStyleName  =  &#39; oneUserDefinedStyle &#39; ; 
$ phpWord - > addFontStyle(
 $ fontStyleName, array( &#39; name &#39; => &#39; Tahoma &#39;, &#39; size &#39; => 10, &#39; color &#39; => &#39; 1B2232 &#39;, &#39; bold &#39; => true)); $ section - >    
            
    “最大的成就是不是永远不跌倒,‘ 
’但在再度上涨你掉下去了。” &#39;。&#39;(Vince Lombardi)&#39;,$ fontStyleName);         
         
    
//添加使用明确创建的字体样式对象自定义字体的文本元素... 
$ fontStyle  =  new  \ PhpOffice \ PhpWord \ Style \ Font(); 
$ fontStyle - > setBold( true); 
$ fontStyle - > setName( &#39; Tahoma &#39;); 
$ fontStyle - > setSize( 13); 
$ myTextElement  =  $节- > addText( &#39; “相信你能和你\&#39;重新一半。“(西奥多·罗斯福)&#39;); 
$ myTextElement - > setFontStyle($ fontStyle);
//将文档保存为OOXML文件... 
$ objWriter  =  \ PhpOffice \ PhpWord \ IOFactory :: createWriter( $ phpWord, &#39; Word2007 &#39;); 
$ objWriter - > save( &#39; helloWorld.docx &#39;);
//将文档保存为ODF文件... 
$ objWriter  =  \ PhpOffice \ PhpWord \ IOFactory :: createWriter( $ phpWord, &#39; ODText &#39;); 
$ objWriter - > save( &#39; helloWorld.odt &#39;);
//将文档保存为HTML文件... 
$ objWriter  =  \ PhpOffice \ PhpWord \ IOFactory :: createWriter( $ phpWord, &#39; HTML &#39;); 
$ objWriter - > save( &#39; helloWorld.html &#39;);
/ *注意:我们跳过RTF,因为它不是基于XML的,需要一个不同的例子。* / 
/ *注意:我们跳过PDF,因为“HTML-to-PDF”方法用于创建PDF文档。* /
Copy after login

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.

How to convert php rich text to html

(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 = &#39;pppp&#39;;
$wordname = &#39;files/word/&#39;.$newname.".doc";//生成文件路径
echo $wors;
$this->save($wordname);
ob_flush();//每次执行前刷新缓存
flush();
function start()
{
    ob_start();
    echo &#39;<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>&#39;;
}
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);
}
Copy after login

(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 ε≡٩(๑>₃<)۶ Yixinxiangxue

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(&#39;diary&#39;)->where(&#39;id&#39;,12)->find();
        $title = &#39;<h1 style="text-align: center;">&#39; . $html[&#39;notename&#39;] . &#39;</h1>&#39;;
        $content = $html[&#39;content&#39;];
        $mpdf  =  new Mpdf();
        $mpdf->autoScriptToLang = true;
        $mpdf->autoLangToFont = true;
        $header=&#39;<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>&#39;;
        $mpdf->SetHTMLHeader($header);  //页眉
//        $mpdf -> WriteHTML(&#39; <h1> Hello world!</h1> &#39;);
        $mpdf -> WriteHTML($title.$content);
        $mpdf->Output();
//        $mpdf -> Output(&#39;files/word/one.pdf&#39;,&#39;D&#39;); //存为文件
    }
Copy after login

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 Tutorial

"

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template