Forum Discussion
unicorn
9 years agoRegular Visitor
Robust function to remove HTML tags
Hi, I have a column of HTML data that I need to display as text. I need to get rid of all HTML tags and substitute reserved HTML characters. I was trying to recursively remove all HTML tags first. Th...
- 9 years ago
Just change the "|" into "<"
rmvAll = if Text.PositionOf(rmvOne, "<") >= 0 then @removeAll(rmvOne) else rmvOne
Otherwise you may get unexpected results if the string contains < or > that are not part of a HTML tag.
If you are interested, I can share my code that only removes tag pairs, i.e. </...> strings preceded by the same without /: <...>
stejin
6 years agoNew Member
Here is a different approach that uses the built-in Web.Page function.
Below custom function replace special characters such as or $amp; and additionally parses and combines text in HTML tags.
(Text as any) => let
Source = Text,
Html = Web.Page(Source),
resolve = (t as table) =>
let
Result = List.Accumulate(Table.ToRecords(t), {}, (state, current) => List.Combine({state, if current[Text] <> null then {current[Text]} else if current[Children] <> null then @resolve(current[Children]) else {null} }))
in
Result,
Tables = Html[Data]{0},
Text1 = resolve(Tables),
Text2 = List.Select(Text1, each Text.Length(Text.Trim(_)) > 0),
Text3 = Text.Combine(Text2, Character.FromNumber(10))
in
Text3