breaking strings in pieces

A

Anonymous

Guest
Okay I've got another question that you guyz might have tried before. What I want to do is make from 1 string of 40 chars -> 2 strings of +-20 chars. BUT they may NOT be broken in between a word e.g.

whole string: hello my name is Duke

line 1: hello my na
line 2: me is Duke

it must be like:

line 1: hello my
line 2: name is Duke

so I want it to look if in between the substring of 20 chars there is a space in the last 5 characters of the substring and if so make the line 1 into the chars before that space and make line 2 into everything after that space...

What's the easiest (fastest) way to do this?

thanks :)
 
My guess is that your going to have to make a function that checks if your ending point is a space/null or not, then add more length if necessary.
 
$base_string = 'some string here with words in it';
$space_pos = strpos($base_string,' ',20);
$left_string = substr($base_string,0,$space_pos);
$right_string = substr($base_string,$space_pos);

a bit basic - should work though.
 
thanks for your help, I could work with the example pootergeist gave me :)thanks
 
Back
Top