I am calling recent comments into my template with


the variable length is specified like this :


how could I write a conditional statement just for comments that exceed the limits of the vairable >180 ?

I was thinking It would be good to have a ... for longer comments and a more link.

1 Answer 1


By using a function to perform that:


that function would look like (in case you're using PHP, part of your code looks from another language):

function recent_comment_text_more($comment_content, $more_href)  {
    if (strlen($comment_content) > 180) {
        $comment_content = substr($comment_content, 0, 177) . sprintf('<a href="%s">... (more)</a>', $more_href);
    return $comment_content;

Good luck!

Multibyte charset safe variant

As pointed out in a comment, e.g. for UTF-8, see mb_internal_encoding() for specifying the encoding to use:

function recent_comment_text_more($comment_content, $more_href)  {
    if (mb_strlen($comment_content) > 180) {
        $comment_content = mb_substr($comment_content, 0, 177) . sprintf('<a href="%s">... (more)</a>', $more_href);
    return $comment_content;
  • 2
    Use mb_substr() or multibyte characters may break.
    – fuxia
    Mar 8, 2011 at 6:47
  • I added a quick edit; the shortened content was being stored in $comment_conent, which would have resulted in this not working correctly. Apr 8, 2011 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.