I have a code snippet written in PHP that pulls a block of text from a database and sends it out to a widget on a webpage. The original block of text can be a lengthy article or a short sentence or two; but for this widget I can't display more than, say, 200 characters. I could use substr() to chop off the text at 200 chars, but the result would be cutting off in the middle of words-- what I really want is to chop the text at the end of the last 字在200个字符之前。
发布于 2018-01-05 15:31:40
通过使用wordwrap功能。可以将文本分成多行,
substr($string, 0, strpos(wordwrap($string, $your_desired_width), "\n"));
发布于 2018-01-05 16:51:43
这将返回单词的前200个字符:
preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, 201));
https://stackoverflow.com/questions/-100007117
复制相似问题