ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to count the number of characters in an entry

January 12, 2011 3:30pm

Subscribe [3]
  • #1 / Jan 12, 2011 3:30pm

    artlab

    37 posts

    So I have a situation with some entries in field1 have only 200 words, while others have 500 words.
    I want to change the text size based on the number of words or characters entered into field1.

    So for example:
    If an entry has <200 words then I want the page to render the type larger using a unique style tag.
    If the entry has >200 then it renders the type smaller.

    This lets me keep a fixed size for a block of text which has a fixed size side graphic, and not look like a ton of empty white space.

    Is there an easy PHP snippet or is there a plug-in that returns the number of characters in an entry when I render the page?

    Thanks!

  • #2 / Jan 13, 2011 3:09am

    John Henry Donovan

    12339 posts

    artlab,

    Try the following.

    <?php 
    $str = "This should be useful";
    echo str_word_count($str);
    ?>

    Moving to the CodeShare Corner as more appropriate there

  • #3 / Mar 29, 2011 1:53am

    fensterbaby

    289 posts

    So how would I adapt this as a conditional tag?

    I have a widget on a page that displays a random entry from a channel. But those entries can get to long for the space allowed on some templates.

    So I want some sort of search tag that limits the found set to entries with charCount < n.

    Thanks

    PS - seems like there out to be an EE tag for this. Doesn’t seem like it would be hard to do… well for me but not for EE programmers.

  • #4 / Mar 31, 2011 3:19pm

    fensterbaby

    289 posts

    What I need is something like this… (pseudo code)

    {exp:channel:entries channel="myChannel" search="chrCount'{myField}’< 300” limit=“1”} 

    There must be other folks out there who have a similar need. Is there a function or.. something?

    John’s example above give char count of a string but I’m not a PHP programmer so getting from that to some sort of function is beyond me.

    Thanks

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases