convert text source into variable

时间:2017-04-10 00:37:30

标签: php variables text

i have a text result from database like this:

var1=value1&var2=value2&var3=value3

How to convert into a variable and get the value?

2 个答案:

答案 0 :(得分:4)

$str = "var1=value1&var2=value2&var3=value3";


parse_str($str, $output);

print_r($output);


Array
(
    [var1] => value1
    [var2] => value2
    [var3] => value3
)

manual page:http://php.net/manual/en/function.parse-str.php

答案 1 :(得分:1)

This works too:

<?php
$string = "var1=value1&var2=value2&var3=value3";
$string = explode("&", $string);

foreach($string as $key) {
    $result = strrchr($key,"=");
    $result = trim($result, "=");
    echo $result . "<br>";
    }
?>