0

how can I remove font type from string which is in database and saved with ckeditor? For example:

<div style="font-family: Tahoma; color: Red;">
Foo FOooooo
</div>

I want to remove it or change it to Verdana for example.

I know I can use replace but font names can be different and I know I can use substring method. But is there any easy way to remove it?

2 Answers 2

1

Try this simple way by this regex:

capture font-family and color styles:

<div\s+style=\".*?font-family:(?<fontName>\s*[^;\"]*)?.*?color:(?<color>\s*[^;\"]*)?

and your code for replacement:

String inputStr = "<div style=\"font-family: Tahoma; color: Red;\">";

foreach(Match m in Regex.Matches(inputStr, "<div\\s+style=\\\".*?font-family:(?<fontName>\\s*[^;\\\"]*)?.*?color:(?<color>\\s*[^;\\\"]*)?"))
{
    inputStr = inputStr.Replace(m.Groups["fontName"].Value, "Vernada").Replace(m.Groups["color"].Value, "Blue");
}

explain:

(?<name> subexpression)

Captures the matched subexpression into a named group.

Sign up to request clarification or add additional context in comments.

Comments

1

There are two ways, the easy way, just remove the full div style using a "simple" Regex

 private static Regex oClearHtmlScript = new Regex(@"<(.|\n)*?>", RegexOptions.Compiled);

 public static string StripHTML(string sHtmlKeimeo)
 {
     if (string.IsNullOrEmpty(sHtmlKeimeo))
         return string.Empty;   

     return oClearHtmlScript.Replace(sHtmlKeimeo, string.Empty);
 }

and the hard way, use the Html Agility Pack (or any other similar) to parse the html and direct change the attributes.

http://htmlagilitypack.codeplex.com/

2 Comments

Thanks but if I use this method I clear/remove all html tags. I don't want it, just font-family that I want.
@kad1r then go with the Agility Pack, the second option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.