Jump to content

explode question


centenial

Recommended Posts

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,
Link to comment
https://forums.phpfreaks.com/topic/23494-explode-question/
Share on other sites

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".
Link to comment
https://forums.phpfreaks.com/topic/23494-explode-question/#findComment-106608
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/23494-explode-question/#findComment-106630
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.