Table of Contents
' . $html['notename'] . '
 Hello world!
Home Backend Development PHP Problem How to convert php rich text to html

How to convert php rich text to html

Nov 16, 2021 am 09:34 AM
html php rich text

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 id="nbsp-nbsp-html-notename-nbsp-nbsp">&#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 id="nbsp-Hello-nbsp-world"> 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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles