Works, but one long word (or just a random string of characters, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...) would bypass it.
Works, but one long word (or just a random string of characters, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...) would bypass it.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
//shrug
Anyway, your function probably makes more sense. If anyone wants to use this, you could (or I would) make the ending '...' dynamic.PHP Code:function cut_it($str,$len=20) { //default is 20, but you can switch to anything you want here on when calling it
if (strlen($str)<=$len) { return $str; }
$str = wordwrap($str,$len); //defaults to the behaviour twey setup
$str = explode("\n",$str,2); //lazy-- you could use substr/strpos, etc
$str = $str[0];
if ($str>$len) {
$str = substr($str,0,$len-3).'...';
}
return $str;
}
$test = 'whatever and stuff this is some text etc';
$test = cut_it($test,15);
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Mm. Handy though. Here:Code:function truncate_string($tr, $len, $tail = '...') { if(strlen($tr) > $len) { $tr = explode("\n", wordwrap(str_replace("\n", "\0", $tr), $len), 2); $tr = str_replace("\0", "\n", substr($tr[0], 0, $len) . $tail); } return $tr; }
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Code:function truncate_string($tr, $len, $tail = '...') { if(strlen($tr) > $len) { $len = $len-3; $tr = explode("\n", wordwrap(str_replace("\n", "\0", $tr), $len), 2); $tr = str_replace("\0", "\n", substr($tr[0], 0, $len) . $tail); } return $tr; }
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
No. For a start I just don't think that's a good idea: the actual string will be truncated shorter than the specified number of characters. If I say I want 20 characters of the string to be displayed, I'd be mightily annoyed if it actually got cut down to 17, and that would take me a while to debug if this function was closed off in a black box somewhere. It's just not intuitive.
Secondly, if it were to be done, you'd need strlen($tail) not a static 3, you'd have to take into account what to do if the tail were longer than $len, &c., none of which options are at all intuitive in my opinion.
Thirdly, what's wrong with$len -= 3;?
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
-=3 is fine.
Well, I can see what you mean about it being ambiguous, but having the string longer than the allowed maximum is strange. You have only 20 characters of space, but then need room for the elipses?
And, yes, you're right-- strlen($tail).
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
I might set it to 20 then be a little puzzled when it didn't quite fit, but I'd work it out pretty quickly. If I set it to 20 and only got 17 characters, however, I wouldn't immediately recognise the source of the bug.You have only 20 characters of space, but then need room for the elipses?
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
No... it WOULD Be 20 characters. Do dots not count? You are outputting a string to fit in a small space. (Let's assume this is courier, or another such font, so we don't need to consider [as it would be very difficult anyway, though perhaps a fun task for GD] the width of individual characters].)If I set it to 20 and only got 17 characters
You have 20 spots-- you get 20 characters. Good. You get 23... and... problem.
Though it's only 17 of CONTENT, that still is how much space you have. To maximize content, use no tail.
As for having a tail longer than the character limit... uh... that defeats the point. It's like asking "How many pickup trucks can I fix under my table?"-- and hopefully no one would do that.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Twenty characters of the string.No... it WOULD Be 20 characters. Do dots not count?That's true, but in a real case you'd have to deal with it somehow.As for having a tail longer than the character limit... uh... that defeats the point. It's like asking "How many pickup trucks can I fix under my table?"-- and hopefully no one would do that.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
No. The idea isn't twenty characters of the string, or twenty you like, or twenty you want-- it's twenty that fits within a certain area. If you JUST want the string, go ahead and remove the ellipses... guess that serves no point. So, use no tail, and then you get up to exactly 20 characters.Twenty characters of the string.
In a real case where someone wants to place pickup trucks under a table? There might be someone (probably drunk) who would try such a thing. But it doesn't mean anyone really wants to. If you really want to add a longer string to the end, it's a different situation entirely.That's true, but in a real case you'd have to deal with it somehow.
You could set the function to return an array, the second parameter of which is if the string needs the tail or not (which, if it's longer, would probably actually not need it), then check that and add it manually after in such a situation.
I can't think of a situation where I'd want to have it return the number + tail*, nor one in which I'd want a bizarre tail.
(*Actually, just setting the initial functionc all for thisfunc($str,$len+strlen($tail),$tail) would give a reasonable response because you'd get up to ±3 characters, then, once it was ±3 less than the max length, it would add on the tail. Guess it would work.)
Anyway, there are theoretical needs for making this function handle anything, but there's a reason that the space shuttle doesn't have a propeller and torpedoes.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks