I am trying to get this method in a String Filter working:
public function truncate($string, $chars = 50, $terminator = ' …');
I'd expect this
$in = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890";
$out = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV …";
and also this
$in = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝ";
$out = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđ …";
That is $chars
minus the chars of the $terminator
string.
In addition, the filter is supposed to cut at the first word boundary below the $chars
limit, e.g.
$in = "Answer to the Ultimate Question of Life, the Universe, and Everything.";
$out = "Answer to the Ultimate Question of Life, the …";
I am pretty certain this should work with these steps
However, I have tried various combinations of str*
and mb_*
functions now, but all yielded wrong results. This can't be so difficult, so I am obviously missing something. Would someone share a working implementation for this or point me to a resource where I can finally understand how to do it.
Thanks
P.S. Yes, I have checked https://stackoverflow.com/search?q=truncate+string+php before :)
Just found out PHP already has a multibyte truncate with
mb_strimwidth
— Get truncated string with specified widthIt doesn't obey word boundaries though. But handy nonetheless!
Try this:
function truncate($string, $chars = 50, $terminator = ' …') {
$cutPos = $chars - mb_strlen($terminator);
$boundaryPos = mb_strrpos(mb_substr($string, 0, mb_strpos($string, ' ', $cutPos)), ' ');
return mb_substr($string, 0, $boundaryPos === false ? $cutPos : $boundaryPos) . $terminator;
}
But you need to make sure that your internal encoding is properly set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With