更新时间:2023-02-22 14:40:53
如果字符串太长,可以先用 substr 截断字符串,然后用正则表达式去掉最后一个完整或部分单词:
If the string is too long, you can first use substr to truncate the string and then a regular expression to remove the last full or partial word:
$s = substr($s, 0, (140 - 3));
$s = preg_replace('/ [^ ]*$/', ' ...', $s);
请注意,您必须使原始文件短于 140 个字节,因为当您添加...时,这可能会将字符串的长度增加到 140 个字节以上.
Note that you have to make the original shorter than 140 bytes because when you add the ... this could increase the length of the string beyond 140 bytes.