Saber
Saber

Reputation: 617

get value from exploded string in php

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;

Upvotes: 1

Views: 1816

Answers (6)

prabushitha
prabushitha

Reputation: 1483

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;

?>

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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

Upvotes: 0

brevis
brevis

Reputation: 1201

$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 :)

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

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);

Upvotes: 0

delboy1978uk
delboy1978uk

Reputation: 12365

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

Upvotes: 0

iainn
iainn

Reputation: 17417

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.

Upvotes: 1

Related Questions