All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jflyfox.util.extend.HtmlUtils Maven / Gradle / Ivy

/**
 * Copyright 2015-2025 FLY的狐狸(email:[email protected] qq:369191470).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.jflyfox.util.extend;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * html处理
 * 
 * 2015年2月2日 下午3:39:59 flyfox [email protected]
 */
public class HtmlUtils {

	private static final String regEx_script = "]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	private static final String regEx_style = "]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

	/**
	 * 去除Html标签
	 * 
	 * 2015年2月2日 下午3:40:49 flyfox [email protected]
	 * 
	 * @param htmlStr
	 * @return
	 */
	public static String delHTMLTag(String htmlStr) {
		Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); // 过滤html标签

		return htmlStr.trim(); // 返回文本字符串
	}
	
	/**
	 * 去除Script标签
	 * 
	 * 2017年1月18日 下午3:11:31 flyfox [email protected]
	 * 
	 * @param htmlStr
	 * @return
	 */
	public static String delScriptTag(String htmlStr) {
		Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		return htmlStr.trim(); // 返回文本字符串
	}

	/**
	 * 修改标签特殊字符
	 * 
	 * @请使用changeSpecialCode
	 * 
	 *                       2015年5月18日 下午2:00:35 flyfox [email protected]
	 * @param content
	 * @return
	 */
	public static String changeTag(String content) {
		return changeSpecialCode(content);
	}

	/**
	 * 修改标签特殊字符
	 * 
	 * 2015年5月18日 下午2:00:35 flyfox [email protected]
	 * 
	 * @param content
	 * @return
	 */
	public static String changeSpecialCode(String content) {
		content = content.replaceAll("&", "&");
		content = content.replaceAll("<", "<");
		content = content.replaceAll(">", ">");
		content = content.replaceAll("\"", """);
		content = content.replaceAll("'", "'");
		content = content.replaceAll("/", "/");
		return content;
	}

	/**
	 * 删除标签特殊字符
	 * 
	 * 2015年6月9日 下午9:30:16 flyfox [email protected]
	 * 
	 * @param content
	 * @return
	 */
	public static String delSpecialCode(String content) {
		content = content.replaceAll("&", "");
		content = content.replaceAll("<", "");
		content = content.replaceAll(">", "");
		content = content.replaceAll("\"", "");
		content = content.replaceAll("'", "");
		content = content.replaceAll("/", "");
		return content;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy