Search code examples
phpexplode

Extract items from string and assign them to variables


I need to use php and extract the items from the string and assign them to variables.

$string = "76305203 ;124400884 ;109263187 ;current ;18.44";

How can I get:

$var1 = 76305203
$var2 = 124400884

Solution

  • To create variables use list()

    <?php
    
    $string = "76305203 ;124400884 ;109263187 ;current ;18.44";
    
    list($var1,$var2) = explode(';', $string);
    
    echo $var1;
    
    echo PHP_EOL;
    
    echo $var2;
    

    Output:- https://eval.in/928536

    Or use explode() to get array and use that array

    <?php
    
    $string = "76305203 ;124400884 ;109263187 ;current ;18.44";
    
    $array = explode(';', $string);
    
    echo $array[0];
    
    echo PHP_EOL;
    
    echo $array[1];
    

    Output:-https://eval.in/928537