1. WordPress中轉義HTML與過濾鏈接的相關PHP函數使用解析
esc_html()(轉義
Html)
esc_html()
函數用來轉義
Html
代碼,讓
Html
代碼不轉義。
用法
esc_html(
$text
);
參數
$text
(字元串)(必須)要轉義的字元串。
默認值:None
返回值
(字元串)返回轉義後的字元。
例子
echo
esc_html(
'<a
href="http://www.example.com/">A
link</a>'
);
上邊的代碼將輸出:
WordPress
函數:esc_html()(轉義
Html)
(為了防止瀏覽器轉碼,我直接截了一張圖)
更多
此函數位於:wp-includes/formatting.php
esc_url()(過濾鏈接)
很多
URL
會有一些小錯誤,用
esc_url()
函數可以屏蔽或者修正這些錯誤,並且可以拒絕不安全的協議。
esc_url()
函數的工作內容:
默認拒絕不是下面協議的
URL:defaulting
to
http、https、ftp、ftps、mailto、news、irc、gopher、nntp、feed
和
telnet
刪除無效字元和危險的字元
將字元轉換成
HTML
實體字元
使用方法
esc_url(
$url,
$protocols,
$_context
);
參數
$url
(字元串)(必須)要被過濾的
URL.
默認值:None
$protocols
(數組)(可選)可以接收協議的數組,如果沒有設置,則默認為:defaulting
to
http、https、ftp、ftps、mailto、news、irc、gopher、nntp、feed
和
telnet.
默認值:None
$_context
(字元串)(可選)如何返回
URL.
默認值:(字元串)display
返回值
(字元串)返回過濾後的鏈接。
例子
<?php
echo
esc_url(
'www.endskin.com'
);//輸出:http://www.endskin.com
?>
更多
此函數位於:wp-includes/formatting.php
2. 如何在網頁中過濾html,script,css代碼
<div id="box"></div>
<javascript>
document.getElementById("box").appendChild(newtag);
<javascript>
3. 用php過濾html部分標簽
$str=preg_replace("/\s+/", " ", $str); //過濾多餘回車
$str=preg_replace("/<[ ]+/si","<",$str); //過濾<__("<"號後面帶空格)
$str=preg_replace("/<\!--.*?-->/si","",$str); //注釋
$str=preg_replace("/<(\!.*?)>/si","",$str); //過濾DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str); //過濾html標簽
$str=preg_replace("/<(\/?head.*?)>/si","",$str); //過濾head標簽
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //過濾meta標簽
$str=preg_replace("/<(\/?body.*?)>/si","",$str); //過濾body標簽
$str=preg_replace("/<(\/?link.*?)>/si","",$str); //過濾link標簽
$str=preg_replace("/<(\/?form.*?)>/si","",$str); //過濾form標簽
$str=preg_replace("/cookie/si","COOKIE",$str); //過濾COOKIE標簽
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //過濾applet標簽
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //過濾applet標簽
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //過濾style標簽
$str=preg_replace("/<(\/?style.*?)>/si","",$str); //過濾style標簽
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //過濾title標簽
$str=preg_replace("/<(\/?title.*?)>/si","",$str); //過濾title標簽
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //過濾object標簽
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //過濾object標簽
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //過濾noframes標簽
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //過濾noframes標簽
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //過濾frame標簽
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //過濾frame標簽
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //過濾script標簽
$str=preg_replace("/<(\/?script.*?)>/si","",$str); //過濾script標簽
$str=preg_replace("/javascript/si","Javascript",$str); //過濾script標簽
$str=preg_replace("/vbscript/si","Vbscript",$str); //過濾script標簽
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //過濾script標簽
$str=preg_replace("//si","&#",$str); //過濾script標簽,如javAsCript:alert(
清除空格,換行
function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}
過濾HTML屬性
1,過濾所有html標簽的正則表達式:
復制代碼 代碼如下:
</?[^>]+>
//過濾所有html標簽的屬性的正則表達式:
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);
3,過濾部分html標簽的正則表達式的排除式(比如排除<p>,即不過濾<p>):
復制代碼 代碼如下:
</?[^pP/>]+>
4,過濾部分html標簽的正則表達式的枚舉式(比如需要過濾<a><p><b>等):
復制代碼 代碼如下:
</?[aApPbB][^>]*>
5,過濾部分html標簽的屬性的正則表達式的排除式(比如排除alt屬性,即不過濾alt屬性):
復制代碼 代碼如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,過濾部分html標簽的屬性的正則表達式的枚舉式(比如alt屬性):
復制代碼 代碼如下:
(\s)alt=[^\s]*
4. asp.net過濾html代碼
^// 這一行在頁面頂加入 validateRequest=false
public static string NoHTML(string Htmlstring) //替換HTML標記
{ //刪除腳本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//刪除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
這個方法調用一下就可以的!
5. 如何在WordPress評論中禁用HTML
事實上,手動禁用HTML的操作並不復雜。你只需要完成幾個簡單的操作步驟基本就能完成了。首先,你要先登入你的控制面板,然後進入頁面找到主題目錄。打開當前主題下的functions.php 文件,再在文件中加上如下代碼。
完成以上這幾步操作,HTML就會在你的評論中自動禁用了,然後你所有評論中都不會出現標簽,只有純文本了。
使用插件禁用HTML
如果你覺得以上幾步操作很復雜或者說你覺得手動進行這些設置很麻煩,你就可以使用插件來禁用HTML。
6. wordpress 去掉文本里的html標簽
解決方法如下
方法一: 將wp-includes文件夾下的kses.php中的2句話屏蔽掉即可,如
//add_action('init', 'kses_init'); //add_action('set_current_user', 'kses_init');
上面這種做法是去掉wordpress對所有標簽的過濾,但將這些開放給注冊用戶會存在潛在的隱患,不推薦使用!
方法二: 把自己想要不被過濾的標簽添加到「不過濾的白名單中」!
具體操作: 在wp-includes文件夾下的kses.php中搜索 $allowedposttags,這是個不過濾標簽組成的數組,裡面所列即是不過濾的標簽集合,假如你發表文章時帶有style、script兩組標簽,又不想被不過濾,可在$allowedposttags中添加下面語句:
'style' => array(), 'script' => array()
將上面兩句加入 $allowedposttags的一維數組(即最外面那層array)中即可!!
添加全局變數$allowedposttags的值,添加你所需要啟用的標簽。這種方法將只對提交的文章啟用標簽。
如此修改,每次升級wp以後還需要驗證代碼的有效性(代碼的結構有可能變化),然後需要重新修改。總得來說原則是能使用插件就盡量不修改源代碼。
7. WordPress寫文章時 html代碼無法正常顯示
推薦答案 完全是在胡扯,跟WordPress完全不搭調!
WordPress的文章編輯框右上角可以切換編輯模式,一個是」可視化「 , 一個是」HTML「
你在可視化下填的HTML代碼,WordPress會自動轉義的,可以正常顯示出來!
你可以試試在可視化模式下輸入以下HTML代碼:
<a href="#">123</a>
切換到HTML模式,你將會看到:
<a href="#">123</a>
這樣WordPress是可以顯示你的代碼的!
8. php過濾多餘html標簽的代碼!
$str = 」 This line contains\tliberal \r\n use of whitespace.\n\n」;
$str = trim($str);// 首先去掉頭尾空格
$str = preg_replace(』/\s(?=\s)/』, 『』, $str);// 接著去掉兩個空格以上的
$str = preg_replace(』/[\n\r\t]/』, 『 『, $str);// 最後將非空格替換為一個空格
使用上面的例子可以去掉所有多餘的空格。
首先使用TRim()去頭尾空格,
接著用preg_replace()去掉重復的空格。
當中的(?=)表示只匹配後面的空格跟隨前面的空格的空格
9. 怎麼過濾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;
}
10. asp.net如何過濾掉html代碼
Asp.net中如何過濾html,js,css代碼
以下為引用的內容:
#region/// 過濾html,js,css代碼
/// <summary>
/// 過濾html,js,css代碼
/// </summary>
/// <param name="html">參數傳入</param>
/// <returns></returns>
public static 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(@" no[\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;
}
#endregion
#region /// 過濾p /p代碼
/// <summary>
/// 過濾p /p代碼
/// </summary>
/// <param name="html">參數傳入</param>
/// <returns></returns>
public static string InputStr(string html)
{
html = html.Replace(@"\<img[^\>]+\>", "");
html = html.Replace(@"<p>", "");
html = html.Replace(@"</p>", "");
return html;
}
#endregion