asp.net過濾html標簽只保留換行與空格的方法

字號:


    本文實例講述了asp.net過濾html標簽只保留換行與空格的方法。分享給大家供大家參考。具體分析如下:
    自己從網(wǎng)上找了一個過濾html標簽的方法,我也不知道誰的才是原創(chuàng)的,反正很多都一樣。我把那方法復制下來,代碼如下:
    復制代碼 代碼如下:/// <summary>
    /// 去除html標記
    /// </summary>
    /// <param name=nohtml>包括html的源碼 </param>
    /// <returns>已經(jīng)去除后的文字</returns>
    public static string nohtml(string htmlstring)
    {
    //刪除腳本
    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, @&(copy|#169);, \xa9,
    regexoptions.ignorecase);
    htmlstring = regex.replace(htmlstring, @&#(\d+);, ,
    regexoptions.ignorecase);
    htmlstring.replace(<, );
    htmlstring.replace(>, );
    htmlstring.replace(\r\n, );
    htmlstring = httpcontext.current.server.htmlencode(htmlstring).trim();
    return htmlstring;
    }
    以上代碼是從網(wǎng)上直接復制過來的,這個確實能過濾掉所有的html標簽,但是這個不是我想要的,這個過濾得太干凈了,我如果用textarea輸入框的話,我是要保留空格跟換行的。
    然后我就自己改了一下這個方法,textarea的換行是\n,所以我得把這些標簽重新匹配替換成<br>,這樣的話從數(shù)據(jù)庫中讀取到頁面時,就能正確的換行了,把空格替換成html的空格符,大功告成。
    復制代碼 代碼如下:/// <summary>
    /// 去除html標記(保留br跟\r\n)
    /// </summary>
    /// <param name=nohtml>包括html的源碼 </param>
    /// <returns>已經(jīng)去除后的文字</returns>
    public static string newnohtml(string htmlstring)
    {
    //htmlstring.replace(, %r%n).replace(<br>,%br%).replace(<br/>,%br&%).replace();
    //刪除腳本
    htmlstring = regex.replace(htmlstring, @<script[^>]*?>.*?</script>, ,
    regexoptions.ignorecase);
    //刪除html
    htmlstring = regex.replace(htmlstring, @<(.[^>]*)>, ,
    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, @&(copy|#169);, \xa9,
    regexoptions.ignorecase);
    htmlstring = regex.replace(htmlstring, @&#(\d+);, ,
    regexoptions.ignorecase);
    htmlstring.replace(<, );
    htmlstring.replace(>, );
    //htmlstring.replace(\r\n, );
    htmlstring = httpcontext.current.server.htmlencode(htmlstring);
    htmlstring = regex.replace(htmlstring, @((\r\n)), <br>);
    htmlstring = regex.replace(htmlstring, @(\r|\n), <br>);
    htmlstring = regex.replace(htmlstring, @(\s), );
    return htmlstring;
    }
    這個過濾可以用于讓用戶輸入發(fā)布內容時的過濾。
    希望本文所述對大家的asp.net程序設計有所幫助。