I have a string:
我有一个字符串:
$str = 'Hello World, Welcome World, Bye World';
I want to cut above string into pieces. Each piece should be of 10 characters. If a word is going to be cut, then move that word to next line.
我想把上面的字符串切成碎片。每件应该是10个字符。如果要剪切一个单词,则将该单词移动到下一行。
For Example:
例如:
$output = array();
$output[0] = 'Hello ';
$output[1] = 'World, ';
$output[2] = 'Welcome ';
$output[3] = 'World, Bye';
$output[4] = 'World';
Is there a shortest way without so many if else and loops.
没有那么多if else和循环是否有最短路径。
Thanks
谢谢
3 个解决方案
#1
8
Use wordwrap
. By default it wraps whole words and does not cut them into pieces.
使用wordwrap。默认情况下,它会包装整个单词,而不会将它们分成几部分。
echo wordwrap('Hello World, Welcome World, Bye World', 10);
If you want an array, explode afterwards:
如果你想要一个数组,之后会爆炸:
print_r(explode("\n", wordwrap('Hello World, Welcome World, Bye World', 10)));
#2
0
string test = 'Hello World, Welcome World, Bye World';
string[] splited = test.Split(' ');
foreach (string str in s.Split(splited))
{
Console.WriteLine(str);
}
Note: Its written in C#, I hope it will give you an Idea.
注意:它是用C#编写的,我希望它会给你一个想法。
Regards
问候
#3
0
Although not exactly the answer, some similar problem is like this:
虽然不完全是答案,但是类似的问题是这样的:
You have a long string, you want to break that string into chunks. You prefer not to break words, but words can be broken if word is very long.
你有一个很长的字符串,你想把这个字符串分成几个块。你不想打破单词,但如果单词很长,单词就可以被打破。
$string = "Hello I am a sentence but I have verylongwordthat I can split";
You will split the words in this sentence if word is very long, like this:
如果单词很长,你会在这句话中拆分单词,如下所示:
$pieces = explode(" ",$string);
$textArray=array();
foreach ($pieces as $p) {
$textArray= array_merge($textArray, str_split($p, 10));
}
$stringNew=implode(" ",$textArray);
output:
输出:
"Hello I am a sentence but I have verylongwo rdthat I can split"
#1
8
Use wordwrap
. By default it wraps whole words and does not cut them into pieces.
使用wordwrap。默认情况下,它会包装整个单词,而不会将它们分成几部分。
echo wordwrap('Hello World, Welcome World, Bye World', 10);
If you want an array, explode afterwards:
如果你想要一个数组,之后会爆炸:
print_r(explode("\n", wordwrap('Hello World, Welcome World, Bye World', 10)));
#2
0
string test = 'Hello World, Welcome World, Bye World';
string[] splited = test.Split(' ');
foreach (string str in s.Split(splited))
{
Console.WriteLine(str);
}
Note: Its written in C#, I hope it will give you an Idea.
注意:它是用C#编写的,我希望它会给你一个想法。
Regards
问候
#3
0
Although not exactly the answer, some similar problem is like this:
虽然不完全是答案,但是类似的问题是这样的:
You have a long string, you want to break that string into chunks. You prefer not to break words, but words can be broken if word is very long.
你有一个很长的字符串,你想把这个字符串分成几个块。你不想打破单词,但如果单词很长,单词就可以被打破。
$string = "Hello I am a sentence but I have verylongwordthat I can split";
You will split the words in this sentence if word is very long, like this:
如果单词很长,你会在这句话中拆分单词,如下所示:
$pieces = explode(" ",$string);
$textArray=array();
foreach ($pieces as $p) {
$textArray= array_merge($textArray, str_split($p, 10));
}
$stringNew=implode(" ",$textArray);
output:
输出:
"Hello I am a sentence but I have verylongwo rdthat I can split"