Mohamed Said
Mohamed Said

Reputation: 4613

an array of parameter values

function test()
{   
$content = "lang=en]text en|lang=sp]text sp";
$atts = explode('|', $content);
}

What I'm trying to do is to allow myself to echo $param[en] to get "text en", $param[sp] to get "text sp". Is that possible?

Upvotes: 0

Views: 49

Answers (3)

Gaurav
Gaurav

Reputation: 28755

if this is not hard coded string in $content

function test()
{   
   $content = "lang=en]text en|lang=sp]text sp"; 
   $atts = explode('|', $content);
   foreach($atts as $att){
      $tempLang = explode("]", $att);
      $params[array_pop(explode("=", $tempLang[0]))] = $tempLang[1];
   }

   var_dump($params);
}

Upvotes: 1

wonderb0lt
wonderb0lt

Reputation: 2053

I think in this case you could use regular expressions.

$atts = explode('|', $content);
foreach ($atts as $subtext) {
    if (preg_match('/lang=(\w+)\](\w+) /', $subtext, $regs)) {
        $param[$regs[0]] = $regs[1];
   }
}

Although it seems that you have a bad database structure if that value comes from a database - if you can edit it, try to make the database adhere to make the database normal.

Upvotes: 0

Val Redchenko
Val Redchenko

Reputation: 581

$param = array();
$langs = explode('|', $content);
foreach ($langs as $lang) {
    $arr = explode(']', $lang);
    $key = substr($arr[0], 5);
    $param[$key] = $arr[1];
}

This is if you are sure $content is well-formatted. Otherwise you will need to put in additional checks to make sure $langs and $arr are what they should be. Use the following to quickly check what's inside an array:

echo '<pre>'.print_r($array_to_be_inspected, true).'</pre>';

Hope this helps

Upvotes: 1

Related Questions