Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2011-09-12 10:44:34
- beechy
- Member
- Registered: 2006-03-02
- Posts: 92
Limit charcters in comment_message and strip HTML tags
I want to display a list of the three most recent comments on the homepage of my website in the following format:
Comment author name | Article title | The actual comment
I am currently using the following code:
<txp:recent_comments limit="3" wraptag="ul" break="li">
<txp:comment_permlink>
<p><txp:comment_name />
| <txp:title /></p>
</txp:comment_permlink>
<txp:comment_message />
</txp:recent_comments>
This works ok – however I want to be able to strip the html from the actual comment and limit the number of characters to 50 otherwise long comments will mess up my design.
Else where on my site I use the following php:
<txp:php>
$limit = 100;
$bod = strip_tags(excerpt());
$bod = preg_replace('/^(.{'.$limit.'}).*$/u', '$1', $bod);
echo $bod;
</txp:php>
This strips the html tags from the article excerpt and limits the number of characters to 100.
How would I modify this to work for the <txp:comment_message /> as per my above code. Or is there a simplier way of doing this.
Thanks in advance.
Last edited by beechy (2011-09-12 10:48:04)
Offline
Re: Limit charcters in comment_message and strip HTML tags
Try rvm_substr which cuts the string, taking into account surrounding tags, leaving well-formed HTML in place.
If you do want extra tag stripping, though, you can also try rah_function to do the strip_tags()
part.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Limit charcters in comment_message and strip HTML tags
Bloke wrote:
Try rvm_substr which cuts the string, taking into account surrounding tags, leaving well-formed HTML in place.
If you do want extra tag stripping, though, you can also try rah_function to do the
strip_tags()
part.
Nice, nice. Yep, would work.
For example by using rah_function. Rah_function strips tags and cuts exactly at 50 character mark.
<txp:rah_function call="mb_substr" start="0" length="50" encoding="UTF-8">
<txp:rah_function call="strip_tags">
<txp:comment_message />
</txp:rah_function>
</txp:rah_function>
Or rah_function and rvm_substr together. Rah_function does stripping, rvm_substr handles truncating.
<txp:rvm_substr length="50">
<txp:rah_function call="strip_tags">
<txp:comment_message />
</txp:rah_function>
</txp:rvm_substr>
Or rvm_substr alone, no stripping is done.
<txp:rvm_substr length="50">
<txp:comment_message />
</txp:rvm_substr>
Last edited by Gocom (2011-09-12 11:35:21)
Offline
#4 2011-09-12 12:27:42
- beechy
- Member
- Registered: 2006-03-02
- Posts: 92
Re: Limit charcters in comment_message and strip HTML tags
Hey guys, these works perfectly – many thanks.
Offline