、
為換行,清除其餘標籤
public static String stripHtml(String content) {
// 1. 段落標籤轉換為換行
content = content.replaceAll("<p .*?>", "\r\n");
// 2. 換行標籤轉換為換行
content = content.replaceAll("<br\\s*/?>", "\r\n");
// 3. 移除其餘所有標籤
content = content.replaceAll("\\<.*?>", "");
// 4. 可選:還原 HTML 實體字元(需引入 HTMLDecoder 或自行實作)
// content = HTMLDecoder.decode(content);
return content;
}
`、`
`)轉為換行,保留段落結構。
Jsoup、Jericho HTML Parser 等專業解析器。