[PHP] Re: exploding
M. Sokolewicz wrote: Jake McHenry wrote: Hi everyone. Is there a way to explode by every character in the variable? Example: $var = 8; $test = explode(, $var); output would be $test[0] = 0; $test[1] = 0; $test[2] = 0; $test[3] = 0; $test[4] = 8; Can I get an array like that? Thanks, Jake McHenry MIS Coordinator Nittany Travel http://www.nittanytravel.com 570.748.6611 x108 well, you can access strings like arrays already. So instead of exploding you can already do something like $var = 15 is a number; $var[2] = 7; echo $var; // will output: 17 is a number same way goes for accessing them In PHP5, the square brackets have been replaced with the curly ones (the square brackets still works, but it's deprecated) $string = 12345; echo $string{2}; // 3 -- Daniel Schierbeck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: exploding
Jake McHenry wrote: Hi everyone. Is there a way to explode by every character in the variable? Example: $var = 8; $test = explode(, $var); output would be $test[0] = 0; $test[1] = 0; $test[2] = 0; $test[3] = 0; $test[4] = 8; Can I get an array like that? Thanks, Jake McHenry MIS Coordinator Nittany Travel http://www.nittanytravel.com 570.748.6611 x108 well, you can access strings like arrays already. So instead of exploding you can already do something like $var = 15 is a number; $var[2] = 7; echo $var; // will output: 17 is a number same way goes for accessing them -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: exploding
Hi, On Wed, 25 Aug 2004 18:00:49 -0400, in php.general [EMAIL PROTECTED] (Jake McHenry) wrote: Hi everyone. Is there a way to explode by every character in the variable? Example: $var = 8; $test = explode(, $var); Use preg_split(): http://php.net/preg_split - example 2. ?php $var = 8; $test = preg_split('//', $var,-1,PREG_SPLIT_NO_EMPTY); print_r($test); ? Or, a possible faster method, using str_split() (only available in PHP5): http://php.net/str_split ?php $var = 8; $test = str_split($var); // requires PHP5 print_r($test); ? -- - Peter Brodersen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: exploding
Try this: $array = explode( ' ', chunk_split( $sting, 1, ' ' ) ); http://www.php.net/manual/de/function.explode.php http://www.php.net/manual/de/function.chunk-split.php On Thu, 26 Aug 2004 00:50:09 +0200, Peter Brodersen [EMAIL PROTECTED] wrote: Hi, On Wed, 25 Aug 2004 18:00:49 -0400, in php.general [EMAIL PROTECTED] (Jake McHenry) wrote: Hi everyone. Is there a way to explode by every character in the variable? Example: $var = 8; $test = explode(, $var); Use preg_split(): http://php.net/preg_split - example 2. ?php $var = 8; $test = preg_split('//', $var,-1,PREG_SPLIT_NO_EMPTY); print_r($test); ? Or, a possible faster method, using str_split() (only available in PHP5): http://php.net/str_split ?php $var = 8; $test = str_split($var); // requires PHP5 print_r($test); ? -- - Peter Brodersen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: exploding
On Thu, 26 Aug 2004 00:57:43 +0200, in php.general [EMAIL PROTECTED] (Felix) wrote: $array = explode( ' ', chunk_split( $sting, 1, ' ' ) ); http://www.php.net/manual/de/function.explode.php http://www.php.net/manual/de/function.chunk-split.php Well, that would leave a blank entry end of the array: array(6) { [0]= string(1) 0 [1]= string(1) 0 [2]= string(1) 0 [3]= string(1) 0 [4]= string(1) 8 [5]= string(0) } Besides, strings containing spaces could also produce unexpected results (like foo bar baz): array(14) { [0]= string(1) f [1]= string(1) o [2]= string(1) o [3]= string(0) [4]= string(0) [5]= string(1) b [6]= string(1) a [7]= string(1) r [8]= string(0) [9]= string(0) [10]= string(1) b [11]= string(1) a [12]= string(1) z [13]= string(0) } I would still suggest str_split() if available, otherwise preg_split(). -- - Peter Brodersen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] RE: exploding strings
heh does the job :) ?php $no=(1+2+3); $number=explode(+, $no); foreach ($number as $key=$value){ if ($value==1) { print 'span style=color:red;'.$value.'/span'; } } ? -Original Message- From: Michael P. Carel [mailto:[EMAIL PROTECTED]] Sent: Wednesday, November 20, 2002 5:54 AM To: Subject: exploding strings Hi to all, I have a problem regarding exploding string. I want to explode string sepated by + sign, but i want it also the first variable in the array be distinc in terms of collor or whatever when viewed in the page. ex. $no=(1+2+3); $number=explode(+, $no); how could i echo $no with the first no. 1 be in colored. Any idea. thanx in advance Regards, mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: exploding values into new variables
Petre Agenbag wrote: Hi The manual states: $pizza = piece1 piece2 piece3 piece4 piece5 piece6; $pieces = explode( , $pizza); So, if I want to be able to echo piece1 to the screen at a later stage, I would go: echo $pieces[0]; or $first_piece = $pieces[0]; echo $first_piece; Right? Right! Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php