1. 過濾所有html標簽的幾種方法
<!DOCTYPE html>
<html lang="en">
<head>
屬<meta charset="UTF-8">
<title>test</title>
<script type="text/javascript">
window.onload = function() {
var oTxt1 = document.getElementById('txt1');
var oTxt2 = document.getElementById('txt2');
var test = document.getElementById('test');
test.onclick = function() {
var reg = /<[^<>]+>/g;
oTxt2.value = oTxt1.value.replace(reg, '');
};
}
</script>
</head>
<body>
<div>
<input type="text" id="txt1">
<input type="text" id="txt2">
</div>
<div><button id="test">測試</button></div>
</body>
</html>
2. Thinkphp 過濾HTML標簽
經過截獲http的請求數據發現轉義是發生在thinkphp接收html文本之前由瀏覽器或在線編輯版器自動權轉義的。
對於使用create方法時可以在Model文件夾中定義模型類,在模型類中定義(content是你提交的欄位):
protected $_auto = array(
array('content', 'htmlspecialchars_decode', self::MODEL_BOTH, 'function'),
);
然後用D("模型名")->create();
如果是用I函數接收的可以改為$content = I('content', '', 'htmlspecialchars_decode');
3. 如何過濾HTML標簽,或者讀取數據時,去處HTML標簽
如果你把html標簽除掉了問題會更大。
如果你不需要所見即所得的編輯器,那麼可以直接使用textarea。在把用戶輸入的html標簽過濾掉就行了。
4. 怎樣用js方法過濾html等代碼
^<input type="text" id="theOne" value="">
<input type="button" onclick="NoHtml()" value="過濾html標簽">
<script>
function NoHtml(){
var t=document.getElementById("theOne").value;
t=t.replace(/({|})/g,''); //過濾{}
t=t.replace(/</g,'<'); //置換符號<
t=t.replace(/>/g,'>'); //置換符號>
// t=t.replace(/<\/?[^>]*>/g,''); //*<\/?[^>]*>可以匹配<script></style></body>等,並置空。而不是替內換容<和>兩個符號
document.getElementById("theOne").value=t;
}
</script>
5. 怎麼過濾html標簽
過濾html標簽代碼如下:
public string checkStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //過濾<script></script>標記
html = regex2.Replace(html, ""); //過濾href=javascript: (<A>) 屬性
html = regex3.Replace(html, " _disibledevent="); //過濾其它控制項的on...事件
html = regex4.Replace(html, ""); //過濾iframe
html = regex5.Replace(html, ""); //過濾frameset
html = regex6.Replace(html, ""); //過濾frameset
html = regex7.Replace(html, ""); //過濾frameset
html = regex8.Replace(html, ""); //過濾frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
6. 如何過濾HTML標簽對文本內容進行操作
|用js過濾
function removeHTMLTag(str) {
str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
//str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多餘空行
str=str.replace(/ /ig,'');//去掉
return str;
}
7. 怎麼使用js過濾html標簽
你可以利用正則表達式來剔除這些標簽,也就是將所有的html類的標簽都替換為空即可:
//去除HTML標簽
str=str.replace(/</?[^>]*>/g,'');
8. JS正則過濾指定的HTML標簽
1,得到網頁上的鏈接源地址:
string
matchString =
@"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>";
2,得到網頁的標題:
string matchString = @"<title>(?<title>.*)</title>";
3,去掉網頁中的所有的html標記:
string temp = Regex.Replace(html, "<[^>]*>", ""); //html是一個要去除html標記的文檔
4, string matchString = @"<title>([\S\s\t]*?)</title>";
5,js去掉所有html標記的函數:
function delHtmlTag(str)
{
return str.replace(/<[^>]+>/g,"");//去掉所有的html標記
}
9. 如何監聽驗證一個項目中的所有 input輸入框 自動過濾 html標簽 css樣式 和js腳本之類的
<input onkeyup="usrNameSet(this)" />
其它的自己可以隨便調用
Js部分
//只能輸入數字、字母、小數點、漢字、@
function usrNameSet(num){
var str=num.value;
//var str = document.getElementById("userName").value;
var value=str.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'');
document.getElementById("userName").value=value;
}
//手機號碼、手機驗證碼,只能輸入數字
function numberSet(num){
var str=num.value;
var value=str.replace(/[^0-9]/g,'');
num.value=value;
}
//推廣碼驗證,只能輸入數字跟字母
function spreadCodeSet(num){
var str=num.value;
var value=str.replace(/[^\w\.\/]/g,'');
num.value=value;
}
//密碼設置 不能為空格
function passwordSet(num){
/*
var str=document.getElementById("password").value;
var value=str.replace(/^ +| +$/g,'');
document.getElementById("password").value=value;
*/
var str=num.value;
var value=str.replace(/^ +| +$/g,'');
num.value=value;
}
//登陸驗證碼
//只能是4位字母或數字
function UserVerifycodeSet(){
var str=document.getElementById("verifycode").value;
var value=str.replace(/[^a-zA-Z0-9]/g,'');
document.getElementById("verifycode").value=value;
}
//姓名和身份證認證
function nameNumberSet(num){
if(num.id=='text_name')//姓名 只能輸入漢字
{
var str=num.value;
var value=str.replace(/[^\u4E00-\u9FA5]/g,'');
num.value=value;
}
else if(num.id=='text_idcard'){//身份證 只能是數字和字母
var str=num.value;
var value=str.replace(/[^a-zA-Z0-9]/g,'');
num.value=value;
}
}
//郵箱輸入 //只能輸入數字、字母、小數點、漢字、@、-
function myMailSet(num){
var str=num.value;
var value = str.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.\-\_]/g,'');
num.value=value;
}
//充值輸入設置,小數點後只能兩位
function moneyInput(num){
//var str=num.value;
//alert(str);
//var value=str.replace(function(){
//if(this.value==this.value2)return;if(this.value.search(/^\d*(?:\.\d{0,2})?$/)==-1)this.value=(this.value2)?this.value2:'';else
this.value2=this.value;
//})
//var value=str.toFixed(2);
// var value = str.replace(/[^a-zA-Z0-9]/g,'');
// num.value=value;
if(num.value==num.value2)return;
if(num.value.search(/^\d*(?:\.\d{0,2})?$/)==-1)
num.value=(num.value2)?num.value2:'';
else num.value2=num.value;
}
//地址輸入設置
//只能輸入數字、字母、
function addressSet(num){
var str=num.value;
var value=str.replace(/[^\u4e00-\u9fa5\w]/g,'');
num.value=value;
}
//銀行卡輸入設置
function formatBankNoSet(BankNo){
//alert(BankNo.value);
var str=BankNo.value;
var value=str.replace(/[^0-9]/g,'');
BankNo.value=value;
}
//只能輸入字母、漢字
function cnOrEn(num){
var str=num.value;
var value=str.replace(/[^\a-zA-Z\u4E00-\u9FA5]/g,'');
num.value=value;
}
10. 怎麼過濾掉除了a標簽以外的所有Html標簽
正則匹配
$pattern1 = "/<[aA](.*)>(.*)<\/[aA]>/";
$pattern2 = "/<[^>]+>/";
$pattern3 = "/{{a(.*)}(.*)}/";
$pattern1先替換掉所有的a標簽
然後$pattern2去除所回有<>標簽
$pattern3還原答a標簽