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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.