Page 1 of 1
split after last word
Posted: Fri Sep 13, 2013 11:24 am
by cataIin
Hello. I have this code:
Code: Select all
$text = "Hello. My name is Bond. James Bond.";
$text_split = str_split($text, 20);
var_dump($text_split);
Return:
array
0 => string 'Hello. My name is Bo' (length=20)
1 => string 'nd. James Bond.' (length=15)
How it's possible to get this:
array
0 => string 'Hello. My name is ' (length=18)
1 => string 'Bond. James Bond.' (length=17)
?
I mean, next array to be after last word who fits in 20 characters.
Of course,
$text isn't always the same.
Re: split after last word
Posted: Fri Sep 13, 2013 12:04 pm
by Celauran
You could iterate over the string looking for spaces then slice off substrings. Alternately, you could explode on space and then loop through the array putting it back together and creating a new array element once you approach 20.
Re: split after last word
Posted: Fri Sep 13, 2013 12:07 pm
by cataIin
Celauran wrote:You could iterate over the string looking for spaces then slice off substrings. Alternately, you could explode on space and then loop through the array putting it back together and creating a new array element once you approach 20.
Can you show me an example?

Re: split after last word
Posted: Fri Sep 13, 2013 2:28 pm
by Christopher
One way (untested):
Code: Select all
$words = explode(' ', $text);
$len = 0;
$text_split = array(0=>'');
$n = 0;
foreach ($words as $word) {
if (strlen($text_split[$n]) + strlen($word) + 1 > 20) {
++$n;
$text_split[$n] = '';
}
$text_split[$n] .= $word . ' ';
}
Re: split after last word
Posted: Fri Sep 13, 2013 4:37 pm
by cataIin
Christopher wrote:One way (untested):
Code: Select all
$words = explode(' ', $text);
$len = 0;
$text_split = array(0=>'');
$n = 0;
foreach ($words as $word) {
if (strlen($text_split[$n]) + strlen($word) + 1 > 20) {
++$n;
$text_split[$n] = '';
}
$text_split[$n] .= $word . ' ';
}
Thank you! Code does exactly what I want.
But now I have another problem. I can't figure out how to integrate it into my code. This is what I have:
I tried all combinations, except the right one. For me, the most correct is:
But doesn't work. Where is the problem? Thanks.
Re: split after last word
Posted: Sun Sep 15, 2013 11:38 am
by Christopher
My code generates a $text_split array just like the str_split() function did in your original version. So you could just replace that. Bonus points for making my code a function.
Re: split after last word
Posted: Sun Sep 15, 2013 3:35 pm
by cataIin
I still can't solve this.

Now I've tried:
And, ofc: Warning: str_split() expects parameter 1 to be string, array given.
So I tried:
Where is the problem in my code?

Ty!
Re: split after last word
Posted: Sun Sep 15, 2013 9:19 pm
by Celauran
The error should give you a hint.
str_split() expects a string to be passed in, not an array. $text_split is clearly an array.
Re: split after last word
Posted: Mon Sep 16, 2013 12:30 am
by Christopher
Why are you still calling str_split() when your initial post was asking for someone to write code to replace str_split() ?!?
Code: Select all
$text_split2 = implode($text_split);
$text_pl = str_split($text_split, 100);
I split the text on word boundaries like you wanted, so you put it back together like it was originally and then split it again like you didn't want it ?!?

Re: split after last word
Posted: Mon Sep 16, 2013 3:46 am
by cataIin
I found the right solution in the end. Many thanks for the code and explanation!
I hope it's okay if I edit the code (which is not directly related to the solution for my problem).

Re: split after last word
Posted: Fri Sep 20, 2013 5:58 am
by priyankagound
Couple ways you can go about it.
Array operations:
$string ="one two three four five";
$words = explode(' ', $string);
$last_word = array_pop($words);
$first_chunk = implode(' ', $words);
String operations:
$string="one two three four five";
$last_space = strrpos($words, ' ');
$last_word = substr($string, $last_space);
$first_chunk = substr($string, 0, $last_space);`