Page 1 of 1

split a string specificly...

Posted: Mon Nov 04, 2002 9:54 pm
by mindmeddler2002
ok...

I got a string

$string1

nice string huh
well $string1 = "This is a string that is a lot longer that this, sometimes about the equivelent of 20 pages, blah blah, blah....";

anyways... how can i split the string after so many characters, and make sure its at a space...

so split at $charnum
if $charnum = " "
if not $charnum = $charnum + (or -) 1?


(i am making a template program for a news poster... so the writers can choose the layout that best fits, and istead of holding the info in up to 4 separate strings, i would rather store it in one, then just split it into the rigth amout of sections when i output it....)

thanks =)

Posted: Tue Nov 05, 2002 4:31 am
by volka
int strpos ( string haystack, string needle [, int offset])
you may test the string's length. If it is longer then x characters you may search with strpos for the next space after that limit (or something before, as you like).
Then you can use substr to split the string.

Posted: Tue Nov 05, 2002 5:43 am
by DeGauss
chunk_split ( string body [, int chunklen [, string end]])

chunk_split($string,number_of_characters_to_split_at,"seperator");

For Example:

$blah=chunk_split("I LIKE PIE!",2,":");

would return as:

I :LI:KE: P:IE:!

(or something like that)

Posted: Tue Nov 05, 2002 5:58 am
by volka
hm.. chunk_split, somthing new for my notes, sounds useful ;)
But it does not assure
and make sure its at a space...

Posted: Tue Nov 05, 2002 6:03 am
by DeGauss
There are various other chunky methods out there, i've only really used chunk_split before, but i think there's a more precise method to it.

Hmm.

perhaps explode the string at every "\r\n" (depending on what platform it's on), and then do an ereg for ". " (a period then a space).

Pleh. Splitting stuff is always an iffy subject, especially if you want to do it precisely with fairly random data.

Posted: Tue Nov 05, 2002 6:20 am
by volka
yo, even more in C or another language where you care more about speed, memory and other nonsense ;)

one more thing

Posted: Tue Nov 05, 2002 6:29 am
by mindmeddler2002
Well when i split the string...

Depending on the templat, i need to split the string in 2 - 4 sections..

Not Even...

anyways, i need to save these to different varabiels

$part1 might be the closest space to 123 characters.
while $part2 might be the closest space to 240 charaters.

ect ect ect

suggestions for this?

let me elaborate

Posted: Tue Nov 05, 2002 6:33 am
by mindmeddler2002
the closest space after i i split the string the first time.

so it i was to split it at 25, 150, 300.

and the string was made up of 500
it would be split in 4 parts

split at first 25
then again at 175 (the 150 + the 25 taken away already, but not showing that first 25)
then again at 475 (split from where the last one ended, at 175, so its only showing 300).
this means the remaining stuff if actually just 25 and not 200....

wrap ur mind around it?

Posted: Tue Nov 05, 2002 6:48 am
by volka
e.g.
given the string S and the number of parts N the string is to be splitted in(english grammar and style correction welcome ;) )
  • while N > 1
  • determine length of S
  • devide by N :arrow: prefered split-position p
  • from that position search fore- and backwards for whitespace (unfortunatly strrpos doesn't accept a int offset parameter)
  • what ever is nearer to p is your split-position
  • take the substring before that position (substr) and store it, make S the remaining substring
  • decrease N by one
totaly untested, errors are for free ;)

by george i think he got it

Posted: Tue Nov 05, 2002 7:09 am
by mindmeddler2002
yep totally untested, if i did not have a 7 page paper due in a hour, i would test it....

bye george i think he got it....

sounds confusing
your crazy
but u also might be a genius...

i will try it tonight