有没有一个 PHP 函数可以将字符串分成几部分而不需要切割单词?
我正在尝试找到一个 php 函数,它将接受一个字符串和一个长度数字,并且字符串中的长度将剪切它,但如果它位于单词中间,则不会,只有当它是空格时,并且会检查寻找最近的空间来这样做。
它还会连续执行此操作并返回字符串数组,无论原始字符串有多长(即,如果原始字符串长度约为 240,而我想削减 80 左右,则数组将是 3 个字符串大)。
我找到了几个函数,但没有一个能做到这一点,而且我在创建自己的函数时遇到了麻烦。
I'm trying to find a php function that will take a string and a length number, and at that length in the string will cut it, but not if its in the middle of a word, only if its a space, and will check for the nearest space to do so.
It also would continuously do this and return the array of strings with however long the original string was (IE if the original string was about 240 in length and I wanted to cut around 80, the array would be 3 strings large).
I found a couple of functions but none that does that, and I'm having trouble creating my own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
wordwrap
函数将字符串拆分为最多给定字符数的行。它会处理单词并且不会在单词中间切入(除非告诉它)。
您可以使用它并按行分割字符串:
The
wordwrap
function splits a string in lines of up to a given number of characters long.It takes care of words and won't cut in the middle of a word (unless to tell it to).
You can use it and split the string by lines:
您需要 wordwrap()。
重要的是确保您有
$cut = false
这样它就不会将单词分成两部分(这是默认设置)。You want wordwrap().
The important thing is to make sure you have
$cut = false
so it doesn't slice words into two parts (which is the default setting).使用
wordwrap()
进行换行,然后就可以explode()
换行符上的字符串以数组格式获取它。Use
wordwrap()
to do the wrapping, then you canexplode()
the string on the linebreaks to get it in array format.