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
cataIin
Forum Newbie
Posts: 8 Joined: Fri Sep 13, 2013 11:06 am
Location: localhost
Post
by cataIin » Fri Sep 13, 2013 11:24 am
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.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Sep 13, 2013 12:04 pm
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.
cataIin
Forum Newbie
Posts: 8 Joined: Fri Sep 13, 2013 11:06 am
Location: localhost
Post
by cataIin » Fri Sep 13, 2013 12:07 pm
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?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Fri Sep 13, 2013 2:28 pm
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)
cataIin
Forum Newbie
Posts: 8 Joined: Fri Sep 13, 2013 11:06 am
Location: localhost
Post
by cataIin » Fri Sep 13, 2013 4:37 pm
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.
Last edited by
cataIin on Mon Sep 16, 2013 3:47 am, edited 1 time in total.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Sep 15, 2013 11:38 am
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)
cataIin
Forum Newbie
Posts: 8 Joined: Fri Sep 13, 2013 11:06 am
Location: localhost
Post
by cataIin » Sun Sep 15, 2013 3:35 pm
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!
Last edited by
cataIin on Mon Sep 16, 2013 3:48 am, edited 1 time in total.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Sun Sep 15, 2013 9:19 pm
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Sep 16, 2013 12:30 am
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 ?!?
(#10850)
cataIin
Forum Newbie
Posts: 8 Joined: Fri Sep 13, 2013 11:06 am
Location: localhost
Post
by cataIin » Mon Sep 16, 2013 3:46 am
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
Post
by priyankagound » Fri Sep 20, 2013 5:58 am
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);`