[英]Converting the HTML description to string to be set as meta description
我正在討論網站上。( http://www.wrangle.in/ )。 我添加了主題的HTML格式描述的新功能。 HTML格式的描述保存在數據庫中。 加載主題后,說明將以HTML格式顯示。 但是,即使在我使用以下類從字符串中刪除HTML標簽之后,元描述仍顯示HTML標簽。 但是該課程無法正常工作。 我從網上的某個地方下載了它。 它不會刪除 ,& 等字符。 即使沒有刪除所有標簽。 請告訴我如何使我的HTML描述在META中作為文本描述可見?
/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
/// <summary>
/// Remove HTML from string with Regex.
/// </summary>
public static string StripTagsRegex(string source)
{
return Regex.Replace(source, "<.*?>", string.Empty);
}
/// <summary>
/// Compiled regular expression for performance.
/// </summary>
static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
/// <summary>
/// Remove HTML from string with compiled Regex.
/// </summary>
public static string StripTagsRegexCompiled(string source)
{
return _htmlRegex.Replace(source, string.Empty);
}
/// <summary>
/// Remove HTML tags from string using char array.
/// </summary>
public static string StripTagsCharArray(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}
}
您剛剛刪除的代碼只是HTML標記,不會轉換&
的
的。
使用HttpUtility.HtmlDecode會將它們轉換為可讀字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.