Processing this string with php

I have a text string that looks like

var1=red&var2=green&var3=blue&var4=magenta

How to manipulate this string to isolate the value var2, which in this case is equalgreen

+3
source share
5 answers

I would start with parse_url What looks pretty close to the URL parameter line, which you could use the built-in methods for processing URLs.

+3
source

Use the php parse_str () function to convert it to an array.

+7
source
parse_str($str, $vars);
echo $vars['var2'];
+3

:

parse_str($str,$tmp);
// $tmp['var2'] is now what you're looking for
+3

parse_str /. , .

<?php

$str = 'var1=red&var2=green&var3=blue&var4=magenta';

parse_str($str, $output);

$result = null;
foreach($output as $k => $v){
    if($v == 'green'){
        $result = $k;
        break;
    }
}

?>
+1

Source: https://habr.com/ru/post/1777577/


All Articles