centenial Posted October 10, 2006 Share Posted October 10, 2006 Hi,I have a 'string' that contains three pieces of information. I need to put each of those pieces of information into a separate variable so I can insert them into three seperate fields in a database table.Here is what I'm trying to do:[CODE]<?php$string = explode(';', first,second,third);$i=1;foreach ($string as $variable) { $v_$i = "$variable"; $i++;}// And then use $v_1, $v_2, and $v_3 as variables in a query string.?>[/CODE] Unfortunately this creates a PHP error. Does anyone know how to do this?Thanks for your time, Quote Link to comment Share on other sites More sharing options...
btherl Posted October 10, 2006 Share Posted October 10, 2006 Like this.. when you use explode(), you are creating a new array.[code]$new_array = explode(';', $original_string);$i = 1;foreach ($new_array as $variable) { print "Exploded item $i is $variable\n"; $i++;}[/code]That first line means "Set $new_array to be the parts of $original_string, split by the ';' character". Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 10, 2006 Share Posted October 10, 2006 You can also use the [url=http://www.php.net/list]list()[/url] construct (it's not really a function):[code]<?php$string = 'info 1;info 2;info 3';list($v_1,$v_2,$v_3) = explode(';',$string);echo $v_1.'<br>'.$v_2.'<br>'.$v_3;?>[/code]I would just explode the string into an array and use it that way:[code]<?php$string = 'info 1;info 2;info 3';$v = explode(';',$string);echo '<pre>' . print_r($v,true) . '</pre>';?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.