This function truncates the string, but adds a string to the end, when requested. For example to concenate three dots (...) to a long username.
For example to turn the long username
* johndoe@www.personalweblog.com into johndoe@www.johndo...
Its possible with the current truncate_utf8(), but than you need to do string comparison and some more truncation afterwards, which is not optimal.
And please mark this as wontfix if not appropriate. I do not have time to maintain this patch, so if its not up to standards or so, just close this issue.
Comments
Comment #1
dries commentedYou can update existing code (e.g. like
format_name()) to take advantage of this patch:Also,
$concis not very descriptive as variable name. Maybe call is$postfixinstead?Comment #2
Bèr Kessels commentedI updated the function to use the better name $postfix. Also, I changed the two places where these three dots were appended in a custom way.
Comment #3
gábor hojtsyMy first thought was that this should be integrated into truncate_utf8() and I still think so. Having a separate function seems to be inadequate to me. Also noticed that truncate_utf8() - and as a result, this suggested function - is broken in that the passed length is not properly used. If you have multibyte characters in the string, strlen() will not return the number of chars but the number of bytes used to represent them (except if you have mbstring overrides, but that is not common).
Comment #4
Bèr Kessels commentedGoba,
I do not really understand the second patr of your post. Do you mean that truncate_utf8() is broken, or that truncate_utf8_conc() will not work() ? If the latter, could you please provide some help and tell me what should be changed or where I should look?
Comment #5
dries commentedIntegrating this in the existing
truncate_utf8function sounds like the better solution.Comment #6
gábor hojtsyStrlen() will return the number of bytes used to represent the string, eg. strlen("Bèr") will return 4, contrary to the fact that it seems to be three characters. This makes truncating very much arbitrary, ie. you will get strings of varying length with the same parameters if you have non single byte chars in them. This looks odd if you use the data in a table column or something.
Comment #7
dries commentedNot going to commit this as is. Integrate with the existing function. This should also fix the
strlen()problem.Comment #8
Bèr Kessels commentedThe existing truncate_utf8() uses the same strlen. And what I got from Gobas post, that function is broken too.
So, integrating is the best option, but will not solve the strlen() issue.
Comment #9
gábor hojtsyEither we need to mention that the $length param is completely advisory to the truncate_utf8() function, or someone comes up with an intelligent way to do an strlen on an utf8 string. What I would do for the latter, is to replace all the multibyte sequences with one byte (in a temporary copy of the string), so we can get a real strlen(). But still this does not make it possible to crop the string at a given exact length.
Comment #10
Steven commentedNow that we have a proper drupal_strlen() and drupal_substr() function which count characters, truncate_utf8() is certainly not the place for this. We should indeed cut off based on character length.
Comment #11
robin monks commentedBased on IRC discussion, changed immensely in HEAD already.
Robin
I ♥ Bugz