Jump to content


explode question

  • Please log in to reply
2 replies to this topic

#1 centenial

  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 10 October 2006 - 02:31 AM


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:


$string = explode(';', first,second,third);


foreach ($string as $variable) {
	$v_$i = "$variable";

// And then use $v_1, $v_2, and $v_3 as variables in a query string.


Unfortunately this creates a PHP error. Does anyone know how to do this?

Thanks for your time,

#2 btherl

  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 10 October 2006 - 03:11 AM

Like this.. when you use explode(), you are creating a new array.

$new_array = explode(';', $original_string);
$i = 1;
foreach ($new_array as $variable) {
  print "Exploded item $i is $variable\n";

That first line means "Set $new_array to be the parts of $original_string, split by the ';' character".

#3 kenrbnsn

  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 October 2006 - 04:52 AM

You can also use the list() construct (it's not really a function):
$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;

I would just explode the string into an array and use it that way:
$string = 'info 1;info 2;info 3';
$v = explode(';',$string);
echo '<pre>' . print_r($v,true) . '</pre>';


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users