1

I have custom cms database and get parameter from database and my result this is:

$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"'

but I need get param1 value or other value. I try use explode but my result this is:

$string = explode('|',$param);

result:

array (size=4)
    0 => string 'param1="value1"' 
    1 => string 'param2="value2"' 
    2 => string 'param3="value3"' 
    3 => string 'param4="value4"'
    4 => string 'param5="value4"' 

I need get value this format:

$param->param1 = value1;
3
  • Do you mean you want the array to be like this: array('value1', 'value2' ....) ?? Commented Sep 26, 2017 at 8:14
  • edit my question. I need this format: $param->param1 = value1; Commented Sep 26, 2017 at 8:18
  • 1
    Something as basic as parse_str(str_replace('|', '&', $param), $paramArray); perhaps? And you can then cast $paramArray to an object, $params = (object) $paramArray; Commented Sep 26, 2017 at 8:21

6 Answers 6

1

You also need to explode each of the substrings on =, and then map the results into an array:

$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"';

$params = explode('|', $param);

$results = [];

foreach ($params as $element) {
  list($key, $value) = explode('=', $element, 2);
  $results[$key] = json_decode($value);
}

echo $results['param1']; // value1

The call to json_decode might look a bit out of place here, but it's the quickest way to convert a quoted string into a native PHP string. The additional argument to the second explode call is to limit the result to two variables, in case the value itself contains an equals sign.

Sign up to request clarification or add additional context in comments.

Comments

1

Try parse_str, you can then create an object using the array

<?php
$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"';

$params = array();
parse_str(str_replace('|', '&', $param), $params);
$params = (object) $params;

echo $params->param1;

?>

Comments

0

Nice and easy with a litte regex:

$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"';
$strings = explode('|', $param);
foreach ($strings as $string) {
    preg_match('#param\d="(?<value>.+)"#', $string, $matches);
    var_dump($matches['value']);
}

The regex #param\d="(?<value>.+)"# looks for param followed by a number then an equals, and we create a named capture group with ?<value> in the brackets.

The output from the var_dumps looks like this:

string(6) "value1" 
string(6) "value2" 
string(6) "value3" 
string(6) "value4" 
string(6) "value5"

Try it here https://3v4l.org/bomO7

Comments

0

When you've split the first part using '|', you can convert the rest as though it was a CSV field with '=' as the divider. This deals with quotes and other elements.

<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"';
$parms = explode('|', $param );
$values = [];
foreach ( $parms as $parm ) {
    list($key, $value) = str_getcsv($parm, "=");
    $values[$key] = $value;
}
print_r($values);

Comments

0
$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"';
$param = json_decode('{"' . str_replace(['=', '|'], ['":', ',"'], $param) . '}');
print_r($param);

This is just example. Don't use it in production :)

Comments

0

Use the following approach:

$param = 'param1="value1"|param2="value2"|param3="value3"|param4="value4"|param5="value5"';
$result = [];
foreach (explode('|', $param) as $item) {
    list($k,$v) = explode('=', $item);
    $result[$k] = trim($v, '"');
}
$result = (object) $result;

// as you wanted
print_r($result->param1);   // value1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.