Wednesday 19 December 2007 9:26:02 pm
Hi, In case anyone needs it, here's a quick and dirty template-based method for stripping HTML tags from an XML field. The code takes an article (intro) XML and strips out any "<" and ">" and the text in between.
{def $child_intro_raw=$child.data_map.intro.data_text}
{def $child_intro=""}
{def $split_string=$child_intro_raw|explode("<")}
{foreach $split_string as $this_string_part}
{def $sub_string=$this_string_part|explode(">")}
{set $child_intro=$child_intro|append($sub_string[1])}
{undef $sub_string}
{/foreach}
{* display result *}
<a title="{$child_intro}" href={$child.url_alias|ezurl}>{$child.name|wash()}</a>
It may fail if there are any < or > symbols in the text. If you wanted to strip out a particular tag, it may be possible to change the first to explode function to something like, say, {def $split_string=$child_intro_raw|explode("<p")} You may find that there are blank lines before the basic text you actually want. To get rid of this, change the code above with the if condition shown below.
{foreach $split_string as $this_string_part}
{def $sub_string=$this_string_part|explode(">")}
{if ne($sub_string[1]|count_words(),0)}
{set $child_intro=$child_intro|append($sub_string[1])}
{/if}
{undef $sub_string}
{/foreach}
- Paul
|