1. Replace keywords and change the font color
public static string ReplaceRed(string strtitle, string redkey)
{
if (redkey == "" || redkey == null)
{
return strtitle;
}
else
strtitle = strtitle.Replace(redkey, " " redkey " ");
return strtitle;
}
The disadvantage of this method is: when the dot character is English with upper and lower case, it will be replaced with the upper and lower case of the keyword after changing color, which is not a good experience.
2. Use regular expressions to change the CSS background color
protected string HighlightText(string inputText,string searchWord)
{
System.Text.RegularExpressions.Regex expression = new System.Text.RegularExpressions.Regex(searchWord.Replace(" ", "|" ), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return expression.Replace(inputText,new System.Text.RegularExpressions.MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(System.Text .RegularExpressions.Match m)
{
return "" m.Value "";//Keyword background color
//return "" m.Value "";//Keyword color change
}
This method can be combined with the frontend JS call :
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn