tags:

views:

84

answers:

4

I have a layout that have to show the tags horizontally. I want to be able to limit the amount of characters outputted.

Example - if I set the limit to 14 the following should happen.

Original: Cats, Dogs, Rain
New output: Cats, Dogs, Ra..

Please note that <?php the_tags ?> returns an array. It is everything returned I want limited to 14 characters.

UPDATE In order to remove any confusion I have updated the post with a screenshot displaying why I want to set this limit. This should make it more clear what kind of solution I am searching for alt text

+1  A: 

How about using the substr function:

$string_new = substr($string, 0, 14);
echo $string_new;

You might want to also use the strip_tags function if you there are html tags coming in between your text.

Sarfraz
This doesn't append the `..`
Jason McCreary
@Jason McCreary: Missed that, +1 to you for adding that :)
Sarfraz
+4  A: 

substr

Check it out http://www.php.net/manual/en/function.substr.php

Something like this should work for you:

$tags = implode(', ', $the_tags)
echo substr($tags, 0, 14) . (strlen($tags) > 14) ? '..' : '';

substr will handle just showing 14 characters, then the last part appends the .. when needed.

Jason McCreary
isnt substr just for strings? <?php the_tags() ?> returns an array
Thomas
implode("",$the_tags) will give you a string, @Thomas
Col. Shrapnel
@Thomas, sorry that wasn't in the original post. I have updated my code to reflect this per @Shrapnel
Jason McCreary
@jason - it does not seem to work either way. I think I might need to edit my question to crush any misunderstanding
Thomas
@Thomas, I know what you want. I think the confusion is more around your variables. What is the PHP variable name for the tags array?
Jason McCreary
The problem is `the_tags()` returns an array of HTML links, not just text!
TheDeadMedic
In that case, I think the spec needs to change. My suggestion would be full tags up to a certain point.
Jason McCreary
A: 

This is not very beautiful to cut the words in the middle.
This function will cut your phrase afrer the end of word, but not longer cpecified length.

function short($txt,$length)
{
    $txt = trim(strip_tags($txt));
    if(strlen($txt) > $length)
    {
        $txt = substr($txt,0,$length);
        $pos = strrpos($txt, " ");
        $txt = substr($txt,0,$pos);
        $txt .= "...";
    }
    return $txt;
}

But all answers before are right too.

GOsha
A: 

The trouble with all these answers is that it has not been made clear that the_tags() returns an array of HTML links!

Running substr(implode(', ', get_the_tags(), X) might return something like;

<a href="/tag1/">Tag 1</a>, <a href="/tag2/">Tag 2</a>, <a href="/tag3

You'd be much better off opting for a CSS / front-end solution (as @Gumbo mentioned in the original comments).

That way, you'll still have your link juice and accessibility marked up, without ending up with something like;

<a href="/keyword-tag/">Keyword Tag</a>, <a href="/awesome-keyword/">Awes...</a>
TheDeadMedic