从字符串中获取前100个字符,并注意完整的单词 [英] Get first 100 characters from string, respecting full words

查看:231
本文介绍了从字符串中获取前100个字符,并注意完整的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在这里问过类似的问题,但是我需要知道这种小的调整是否可能.我想将字符串缩短为100个字符,并使用$small = substr($big, 0, 100);这样做.但是,这只需要前100个字符,并不关心它是否会分解一个单词.

I have asked a similar question here before, but I need to know if this little tweak is possible. I want to shorten a string to 100 characters and use $small = substr($big, 0, 100); to do so. However, this just takes the first 100 characters and doesn't care whether it breaks up a word or not.

有什么方法可以占用一个字符串的前100个字符,但要确保您不会打断一个单词?

Is there any way to take up to the first 100 characters of a string but make sure you don't break a word?

示例:

$big = "This is a sentence that has more than 100 characters in it, and I want to return a string of only full words that is no more than 100 characters!"

$small = some_function($big);

echo $small;

// OUTPUT: "This is a sentence that has more than 100 characters in it, and I want to return a string of only"

有没有一种使用PHP的方法?

Is there a way to do this using PHP?

推荐答案

所有您需要做的就是使用:

All you need to do is use:

$pos=strpos($content, ' ', 200);
substr($content,0,$pos ); 

这篇关于从字符串中获取前100个字符,并注意完整的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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