PHP-X字符后在数组中拆分字符串,没有限制的单词限制 [英] PHP - Split string in array after X characters without cut word on limit

查看:65
本文介绍了PHP-X字符后在数组中拆分字符串,没有限制的单词限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符串拆分为x个字符,然后将其放入数组中.但是,如果x在一个单词的中间,我不需要切单词.我希望能将劣等一词分割开来.

I'm trying to split a string after x characters and put it in array. But I need to don't cut word if x is in a middle of a word. What I expect is to split on the word inferior.

我尝试过:

代码

$string = "Helllooooo I'mmm <strong>theeeeee</strong> <em> woooooorrd</em> theeee loooonnngessttt";
$desired_width = 24;

$str = wordwrap($string, $desired_width, "\n");

var_dump($str);
die;

输出

string 'Helllooooo I'mmm
<strong>theeeeee</strong>
<em> woooooorrd</em>
theeee loooonnngessttt' (length=86)

如何将其放置在数组中?还有另一种方法可以做到吗?这和explode()之间的混合吗?谢谢!

How to put it in array ? Is there another method to do that ? a mix between this and explode() ? thanks !

推荐答案

$string = "Helllooooo I'mmm <strong>theeeeee</strong> <em> woooooorrd</em> theeee loooonnngessttt";
$desired_width = 24;

$str = wordwrap($string, $desired_width, "\n");
$arr = explode("\n", $str);
var_dump($arr);
die;

这篇关于PHP-X字符后在数组中拆分字符串,没有限制的单词限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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