将多字节字符串截断为n个字符 [英] Truncate a multibyte String to n chars

查看:98
本文介绍了将多字节字符串截断为n个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使此方法在字符串过滤器中起作用:

I am trying to get this method in a String Filter working:

public function truncate($string, $chars = 50, $terminator = ' …');

我希望如此

$in  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890";
$out = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV …";

还有这个

$in  = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝ";
$out = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđ …";

$chars减去$terminator字符串的字符.

That is $chars minus the chars of the $terminator string.

此外,该过滤器应该在$chars限制以下的第一个单词边界处剪切,例如

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

  • 从最大字符数中减去终止符中的字符数
  • 验证该字符串是否长于计算出的限制,或将其保留原样返回
  • 找到字符串中最后一个空格字符,该字符低于计算的限制以获取单词边界
  • 在最后一个空格处剪切字符串,或者在找不到最后一个空格的情况下计算出限制
  • 将终止符附加到字符串
  • 返回字符串

但是,我现在尝试了str*mb_*函数的各种组合,但是都产生了错误的结果.这不是那么困难,所以我显然缺少了一些东西.有人会为此共享一个可行的实施方案吗?还是将我指向可以最终了解如何实现此目标的资源.

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.

谢谢

P.S.是的,我已经检查过 https://stackoverflow.com/search?q=truncate+string+php 之前:)

P.S. Yes, I have checked https://stackoverflow.com/search?q=truncate+string+php before :)

推荐答案

尝试一下:

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.

这篇关于将多字节字符串截断为n个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆