HP將HTML實(shí)體轉(zhuǎn)換為普通字符,我們可以使用PHP htmlspecialchars_decode()函數(shù)將特殊的HTML實(shí)體轉(zhuǎn)換為正常字符,如&,<,>(即&,<,>)等HTML實(shí)體字符恢復(fù)到正常字符。
下面我們就結(jié)合代碼示例,為大家介紹PHP將HTML實(shí)體轉(zhuǎn)換為普通字符的實(shí)現(xiàn)方法。
代碼示例如下:
<?php $my_str = "I'm good & <b>"boy "</b>."; echo htmlspecialchars_decode($my_str); echo "<br>"; echo htmlspecialchars_decode($my_str, ENT_QUOTES); echo "<br>"; echo $my_str;
輸出結(jié)果如下:
htmlspecialchars_decode函數(shù)表示將特殊的 HTML 實(shí)體轉(zhuǎn)換回普通字符。
說明 :
string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
其參數(shù):string表示要解碼的字符串,flags表示用下列標(biāo)記中的一個(gè)或多個(gè)作為一個(gè)位掩碼,來指定如何處理引號(hào)和使用哪種文檔類型。默認(rèn)為 ENT_COMPAT | ENT_HTML401。
此函數(shù)的作用和 htmlspecialchars() 剛好相反。它將特殊的HTML實(shí)體轉(zhuǎn)換回普通字符。
被轉(zhuǎn)換的實(shí)體有:&, " (沒有設(shè)置ENT_NOQUOTES 時(shí)), ' (設(shè)置了 ENT_QUOTES 時(shí)), < 以及>。
該htmlspecialchars_decode()函數(shù)與htmlspecialchars()將特殊HTML字符轉(zhuǎn)換為HTML實(shí)體的函數(shù)相反。
本篇文章就是關(guān)于PHP將HTML實(shí)體轉(zhuǎn)換為普通字符的方法介紹,簡單易懂,希望對(duì)需要的朋友有所幫助!
以上就是PHP怎么將HTML實(shí)體轉(zhuǎn)換為普通字符的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注其它相關(guān)文章!
更多技巧請(qǐng)《轉(zhuǎn)發(fā) + 關(guān)注》哦!
幾天,我手里的一個(gè)項(xiàng)目需要將富文本的所有 html 標(biāo)簽全部刪除,得到純文本后再存儲(chǔ)到數(shù)據(jù)庫中。在一系列得搜索操作之后,我找到了實(shí)現(xiàn)這個(gè)目的的幾種方法,在這里我分享給大家,當(dāng)你遇到同樣的情況興許也能用的上。
這個(gè)方法是從文本中去除 html 標(biāo)簽最簡單的方法。它使用字符串的方法 .replace(待替換的字符串,替換后的字符串) 將 HTML 標(biāo)簽替換成空值。 /g 是表示替換字符串所有匹配的值,即字符串中所有符合條件的字符都將被替換。
這個(gè)方法的缺點(diǎn)是有些 HTML 標(biāo)簽不能被剔除,不過它依然很好用。
這種方法是完成該問題的最有效的方法。創(chuàng)建一個(gè)臨時(shí) DOM 并給他賦值,然后我們使用 DOM 對(duì)象方法提取文本。
html-to-text 這個(gè)包的功能很全了,轉(zhuǎn)換也有許多的選項(xiàng)比如:wordwrap, tags, whitespaceCharacters , formatters 等等。
安裝:
npm install html-to-text
使用:
最后感謝閱讀,如果此文對(duì)您有幫助,請(qǐng)點(diǎn)贊或添加關(guān)注。
Tika是一個(gè)內(nèi)容分析工具,自帶全面的parser工具類,能解析基本所有常見格式的文件,得到文件的metadata,content等內(nèi)容,返回格式化信息。總的來說可以作為一個(gè)通用的解析工具。特別對(duì)于搜索引擎的數(shù)據(jù)抓去和處理步驟有重要意義。Tika是Apache的Lucene項(xiàng)目下面的子項(xiàng)目,在lucene的應(yīng)用中可以使用tika獲取大批量文檔中的內(nèi)容來建立索引,非常方便,也很容易使用。Apache Tika toolkit可以自動(dòng)檢測(cè)各種文檔(如word,ppt,xml,csv,ppt等)的類型并抽取文檔的元數(shù)據(jù)和文本內(nèi)容。Tika集成了現(xiàn)有的文檔解析庫,并提供統(tǒng)一的接口,使針對(duì)不同類型的文檔進(jìn)行解析變得更簡單。Tika針對(duì)搜索引擎索引、內(nèi)容分析、轉(zhuǎn)化等非常有用。
應(yīng)用程序員可以很容易地在他們的應(yīng)用程序集成Tika。Tika提供了一個(gè)命令行界面和圖形用戶界面,使它比較人性化。在本章中,我們將討論構(gòu)成Tika架構(gòu)的四個(gè)重要模塊。下圖顯示了Tika的四個(gè)模塊的體系結(jié)構(gòu):
每當(dāng)一個(gè)文本文件被傳遞到Tika,它將檢測(cè)在其中的語言。它接受沒有語言的注釋文件和通過檢測(cè)該語言添加在該文件的元數(shù)據(jù)信息。支持語言識(shí)別,Tika 有一類叫做語言標(biāo)識(shí)符在包org.apache.tika.language及語言識(shí)別資料庫里面包含了語言檢測(cè)從給定文本的算法。Tika 內(nèi)部使用N-gram算法語言檢測(cè)。
Tika可以根據(jù)MIME標(biāo)準(zhǔn)檢測(cè)文檔類型。Tika默認(rèn)MIME類型檢測(cè)是使用org.apache.tika.mime.mimeTypes。它使用org.apache.tika.detect.Detector 接口大部分內(nèi)容類型檢測(cè)。內(nèi)部Tika使用多種技術(shù),如文件匹配替換,內(nèi)容類型提示,魔術(shù)字節(jié),字符編碼,以及其他一些技術(shù)。
org.apache.tika.parser 解析器接口是Tika解析文檔的主要接口。該接口從提取文檔中的文本和元數(shù)據(jù),并總結(jié)了其對(duì)外部用戶愿意寫解析器插件。采用不同的具體解析器類,具體為各個(gè)文檔類型,Tika 支持大量的文件格式。這些格式的具體類不同的文件格式提供支持,無論是通過直接實(shí)現(xiàn)邏輯分析器或使用外部解析器庫。
使用的Tika facade類是從Java調(diào)用Tika的最簡單和直接的方式,而且也沿用了外觀的設(shè)計(jì)模式。可以在 Tika API的org.apache.tika包Tika 找到外觀facade類。通過實(shí)現(xiàn)基本用例,Tika作為facade的代理。它抽象了的Tika庫的底層復(fù)雜性,例如MIME檢測(cè)機(jī)制,解析器接口和語言檢測(cè)機(jī)制,并提供給用戶一個(gè)簡單的接口來使用。
實(shí)現(xiàn)word文檔轉(zhuǎn)html
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tika</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
package com.et.tika.controller;
import com.et.tika.convertor.WordToHtmlConverter;
import com.et.tika.dto.ConvertedDocumentDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
public class HelloWorldController {
@RequestMapping("/hello")
public Map<String, Object> showHelloWorld(){
Map<String, Object> map = new HashMap<>();
map.put("msg", "HelloWorld");
return map;
}
@Autowired
WordToHtmlConverter converter;
/**
* Transforms the Word document into HTML document and returns the transformed document.
*
* @return The content of the uploaded document as HTML.
*/
@RequestMapping(value = "/api/word-to-html", method = RequestMethod.POST)
public ConvertedDocumentDTO convertWordDocumentIntoHtmlDocument(@RequestParam(value = "file", required = true) MultipartFile wordDocument) {
log.info("Converting word document into HTML document");
ConvertedDocumentDTO htmlDocument = converter.convertWordDocumentIntoHtml(wordDocument);
log.info("Converted word document into HTML document.");
log.trace("The created HTML markup looks as follows: {}", htmlDocument);
return htmlDocument;
}
}
package com.et.tika.convertor;
import com.et.tika.dto.ConvertedDocumentDTO;
import com.et.tika.exception.DocumentConversionException;
import lombok.extern.slf4j.Slf4j;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
/**
*
*/
@Component
@Slf4j
public class WordToHtmlConverter {
/**
* Converts a .docx document into HTML markup. This code
* is based on <a href="http://stackoverflow.com/a/9053258/313554">this StackOverflow</a> answer.
*
* @param wordDocument The converted .docx document.
* @return
*/
public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) {
log.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename());
try {
InputStream input = wordDocument.getInputStream();
Parser parser = new OOXMLParser();
StringWriter sw = new StringWriter();
SAXTransformerFactory factory = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8");
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(new StreamResult(sw));
Metadata metadata = new Metadata();
metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8");
parser.parse(input, handler, metadata, new ParseContext());
return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString());
}
catch (IOException | SAXException | TransformerException | TikaException ex) {
log.error("Conversion failed because an exception was thrown", ex);
throw new DocumentConversionException(ex.getMessage(), ex);
}
}
}
package com.et.tika.dto;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
*
*/
public class ConvertedDocumentDTO {
private final String contentAsHtml;
private final String filename;
public ConvertedDocumentDTO(String filename, String contentAsHtml) {
this.contentAsHtml = contentAsHtml;
this.filename = filename;
}
public String getContentAsHtml() {
return contentAsHtml;
}
public String getFilename() {
return filename;
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("filename", this.filename)
.append("contentAsHtml", this.contentAsHtml)
.toString();
}
}
package com.et.tika.exception;
/**
*
*/
public final class DocumentConversionException extends RuntimeException {
public DocumentConversionException(String message, Exception ex) {
super(message, ex);
}
}
以上只是一些關(guān)鍵代碼,所有代碼請(qǐng)參見下面代碼倉庫
啟動(dòng)Spring Boot應(yīng)用
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。