split a string specificly...
Moderator: General Moderators
-
mindmeddler2002
- Forum Newbie
- Posts: 21
- Joined: Mon Nov 04, 2002 3:09 pm
- Location: US, PA, Harrisuburg
split a string specificly...
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 =)
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 =)
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.
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.
hm.. chunk_split, somthing new for my notes, sounds useful 
But it does not assure
But it does not assure
and make sure its at a space...
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.
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.
-
mindmeddler2002
- Forum Newbie
- Posts: 21
- Joined: Mon Nov 04, 2002 3:09 pm
- Location: US, PA, Harrisuburg
one more thing
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?
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?
-
mindmeddler2002
- Forum Newbie
- Posts: 21
- Joined: Mon Nov 04, 2002 3:09 pm
- Location: US, PA, Harrisuburg
let me elaborate
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?
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?
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
)

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
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
-
mindmeddler2002
- Forum Newbie
- Posts: 21
- Joined: Mon Nov 04, 2002 3:09 pm
- Location: US, PA, Harrisuburg
by george i think he got it
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
bye george i think he got it....
sounds confusing
your crazy
but u also might be a genius...
i will try it tonight