当达到特定字符数限制时,将文本块修剪到最接近的单词吗? [英] Trimming a block of text to the nearest word when a certain character limit is reached?

查看:95
本文介绍了当达到特定字符数限制时,将文本块修剪到最接近的单词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个问题:过去一定数量的字符后,如何将文本块修剪到最接近的单词.我不是要限制一定数量的单词或字母,而是要限制字母并将其切成最接近的单词.

Here is the question: How would your trim a block of text to the nearest word when a certain amount of characters have past. I'm not trying to limit a certain number words or letters, but limit the letters and cut it off at the nearest word.

说我有两个字符串:

"This is a block of text, blah blah blah"
"this is another block of txt 2 work with"

说我想将其限制为27个字符,即使这些单词内的字符数达到限制,第一行也将以"blah"结尾,第二行将以"txt"结尾.

Say I wanted to limit it to 27 characters, the first line would end at "blah" and the second on would end at "txt" even though the character limits are reached within those words.

是否有解决此问题的干净方法?

Is there any clean solution to this problem?

推荐答案

请参见自动换行函数.

我可能会做类似的事情:

I would probably do something like:

function wrap($string) {
  $wstring = explode("\n", wordwrap($string, 27, "\n") );
  return $wstring[0];
}

(如果您的字符串已经跨越严重的行,请使用其他字符-或模式-进行除"\ n"以外的拆分)

(If your strings already span across severeal lines, use other char - or pattern - for the split other than "\n")

这篇关于当达到特定字符数限制时,将文本块修剪到最接近的单词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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