Forums

Limiting Post Length (3 posts)

  1. MTS
    Member
    Posted 1 year ago #

    I'm in the process of designing a template and I was wondering what I can I use to show only a specific number of words in a post. This template is for displaying profiles of employees in an organization. The goal is to keep employees from writing overly lengthy bios by hardcoding it to only display X number of words from their post. We're not interested in using the "more" tag, either.

    This is similar to using the_excerpt() except that I'm already using that for something else on the site. This is not a request for how to display a word count on their post.

  2. Xephan
    Member
    Posted 1 year ago #

    Depending on how exactly do you want to limit the length, the following functions would work, from the easiest to the most complex

    Limit by pure character length, so you may end up with partial words
    http://us2.php.net/manual/en/function.substr.php

    Limit by words, splitting on space. But this would treat words separated by spaceless punctuation as one.
    http://us2.php.net/manual/en/function.explode.php

    Limit by words, splitting on non-words, it can mistake certain valid characters as non-word but you can specify additional "word" chars to overcome it

    http://us2.php.net/manual/en/function.str-word-count.php

    Split using regular expression, not likely needed since in most cases the previous function would do
    http://us2.php.net/manual/en/function.preg-split.php

    In all cases, you end up with an array of words which you just have to delete excess before imploding it into a string again.

  3. duyuoc
    Member
    Posted 1 year ago #

    Thank you sharing

Topic Closed

This topic has been closed to new replies.

About this Topic