Hi there,
I have an issue with StripHtml method. I use it fro my search results that need to display a few first characters of the page content. It seems to remove html for some records but not all. If the content is within only p tags it work but if I have strong, heading tags or list items, they are still displayed along with the p tags. Any suggestions how to get it to work and remove all html tags?
Here’s my partial view macro:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Examine
@{var searchTerm = Request.QueryString["search"];}
@{var searcher = ExamineManager.Instance.SearchProviderCollection["WebsiteSearcher"];}
@{var results = searcher.Search(searchTerm, true).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.1f);}
<div class="row searchHead">
<div class="col-sm-12">
<h2>Search results for:</h2>
<h3 class="searchTerm">@searchTerm</h3>
</div>
</div>
@{if (results.Count() == 0)
{
<div class="row">
<ul class="searchList col-sm-12">
<p>Sorry, no results found</p>
</ul>
</div>
}
else
{
<div class="row">
<div class="searchList col-sm-12">
@foreach (var result in results)
{
<div class="searchItem">
@if(result.Fields.ContainsKey("pageTitle"))
{
<h3><a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["pageTitle"]</a></h3>
}
else
{
<h3><a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a></h3>
}
@if(result.Fields.ContainsKey("pageContent"))
{
@*var content = result.Fields["pageContent"];*@
string content = result.Fields["pageContent"];
if(content.Length >= 350)
{
@*<p>@content.Substring(0,350)...</p>*@
@:<div>@Umbraco.StripHtml(@content.Substring(0,250))</div>
}
else
{
@*<p>@content</p>*@
@:<div>@Umbraco.StripHtml(@content)</div>
}
}
else
{
<p>No Summary</p>
}
</div>
}
</div>
</div>
}
}
This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/71411-umbracostriphtml-not-removing-all-tags