Web Analytics Made Easy -
StatCounter explode question - CodingForum

Announcement

Collapse
No announcement yet.

explode question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • explode question

    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.
    
    ?>
    Unfortunately this creates a PHP error. Does anyone know how to do this?

    Thanks for your time,

  • #2
    You are trying to use variable variables, and the syntax you have is not correct to do this.

    There is no need to do the foreach loop and have variable variables. After you explode, you can use the results directly in your query. $string[0] is the first part. $string[1] is the second... These can be put directly in a query the same as any variable.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      or if you realy need the variablames like that
      PHP Code:
      list($v_1$v_2$v_3) = explode(';''first;second;third'); 
      Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

      Comment

      Working...
      X