breaking strings in pieces

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

*JaH*
New php-forum User
New php-forum User
Posts: 80
Joined: Fri Jan 31, 2003 4:18 pm

breaking strings in pieces

Postby *JaH* » Tue Mar 11, 2003 9:14 pm

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

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Tue Mar 11, 2003 10:30 pm

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.

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Wed Mar 12, 2003 2:43 am

$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.

*JaH*
New php-forum User
New php-forum User
Posts: 80
Joined: Fri Jan 31, 2003 4:18 pm

Postby *JaH* » Wed Mar 12, 2003 7:46 am

thanks for your help, I could work with the example pootergeist gave me :)thanks


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests