split after last word

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cataIin
Forum Newbie
Posts: 8
Joined: Fri Sep 13, 2013 11:06 am
Location: localhost

split after last word

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: split after last word

Post 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.
User avatar
cataIin
Forum Newbie
Posts: 8
Joined: Fri Sep 13, 2013 11:06 am
Location: localhost

Re: split after last word

Post 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? :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: split after last word

Post 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 . ' ';
}
(#10850)
User avatar
cataIin
Forum Newbie
Posts: 8
Joined: Fri Sep 13, 2013 11:06 am
Location: localhost

Re: split after last word

Post 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:

Code: Select all

code...
I tried all combinations, except the right one. For me, the most correct is:

Code: Select all

code...
But doesn't work. Where is the problem? Thanks.
Last edited by cataIin on Mon Sep 16, 2013 3:47 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: split after last word

Post 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.
(#10850)
User avatar
cataIin
Forum Newbie
Posts: 8
Joined: Fri Sep 13, 2013 11:06 am
Location: localhost

Re: split after last word

Post by cataIin »

I still can't solve this. :(
Now I've tried:

Code: Select all

code...
And, ofc: Warning: str_split() expects parameter 1 to be string, array given.

So I tried:

Code: Select all

code...
Where is the problem in my code? :? Ty!
Last edited by cataIin on Mon Sep 16, 2013 3:48 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: split after last word

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: split after last word

Post 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 ?!?

:drunk:
(#10850)
User avatar
cataIin
Forum Newbie
Posts: 8
Joined: Fri Sep 13, 2013 11:06 am
Location: localhost

Re: split after last word

Post 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). :)
priyankagound
Forum Commoner
Posts: 27
Joined: Thu Sep 19, 2013 2:53 am

Re: split after last word

Post 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);`
Post Reply