Re: breaking up a string (every N chars)

From: Date: Tue, 27 May 2003 02:31:04 +0000
Subject: Re: breaking up a string (every N chars)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-149031@lists.php.net to get a copy of this message
You can do it that way if you don't know the string, too /* UNTESTED */ if ( (strlen($string) % 3) == 0 ) { $x = 0; while ( $x < (strlen($string)-2) { $newString[] = substr($x, 3); $x += 3; } } else { echo "malformed string\n"; } Question is, which way is faster- substr or explode chunk_split??? On Monday 26 May 2003 06:11 pm, Justin French wrote: > if you KNOW the length of the string, you could use substr(): > > $string = "123456789123456"; > > $newString[] = substr($string,0,3); > $newString[] = substr($string,3,3); > $newString[] = substr($string,6,3); > $newString[] = substr($string,9,3); > > or is $string an unknown length? > > > Justin > > on 27/05/03 12:16 PM, zavaboy (zavaboy@hotmail.com) wrote: > > Hi, How can I break a string every N (eg: 3) characters? > > > > //Suppose I have a string... > > $String = "001102203304405"; > > //Now, how do I break this string every 3 characters? > > //So... > > $String[0] = "001"; > > $String[1] = "102"; > > $String[2] = "203"; > > $String[3] = "304"; > > $String[4] = "405"; > > > > Thanks in advance! -- A leader is the wave pushed ahead by the ship. -Leo Nikolaevich Tolstoy

« previous php.general (#149031) next »