I'm using an expression engine field to post code samples using a <pre>
element and [code]
markup, but I'm also using this field as an excerpt. In the excerpt, I want to remove anything inside these pre elements (or stop, somehow, once it reaches the <pre>
element. Anyone know how to do this? Here's my excerpt code:
<p class="excerpt">{exp:trunchtml chars="300"}{code_content}{/exp:trunchtml}</p>
and here's a sample entry containing code:
</p>Blah blah blah Lorem Ipsum</p> <pre>[code]camera.position.set(0,20,35); [/code]</pre>
That's a pretty specific need, no existing plugin I know of will do that. Your two options are:
Create a dedicated excerpt field (really the best in terms of crafting better excerpts and keeping your data structured).
Or, write your own plugin (really not very difficult) which will take the contents you pass it, then
perform some preg_replace()
on it to eliminate your <pre>
elements and all their contents, returning the cleaned result.
So it would look something like this:
<p class="excerpt">
{exp:trunchtml chars="300"}
{exp:my_plugin:replace_pre}{code_content}{/exp:my_plugin:replace_pre}
{/exp:trunchtml}
</p>
This bit of PHP would do the magic for you inside your plugin:
$content = $this->EE->TMPL->tagdata;
preg_replace('/<pre[^>]*>.*?<\/pre>/i', '', $content);
return $content;