split a string specificly...

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

Post Reply
mindmeddler2002
Forum Newbie
Posts: 21
Joined: Mon Nov 04, 2002 3:09 pm
Location: US, PA, Harrisuburg

split a string specificly...

Post 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 =)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post 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)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hm.. chunk_split, somthing new for my notes, sounds useful ;)
But it does not assure
and make sure its at a space...
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yo, even more in C or another language where you care more about speed, memory and other nonsense ;)
mindmeddler2002
Forum Newbie
Posts: 21
Joined: Mon Nov 04, 2002 3:09 pm
Location: US, PA, Harrisuburg

one more thing

Post 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?
mindmeddler2002
Forum Newbie
Posts: 21
Joined: Mon Nov 04, 2002 3:09 pm
Location: US, PA, Harrisuburg

let me elaborate

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
mindmeddler2002
Forum Newbie
Posts: 21
Joined: Mon Nov 04, 2002 3:09 pm
Location: US, PA, Harrisuburg

by george i think he got it

Post 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
Post Reply