SoLoGHoST
SoLoGHoST

Reputation: 2689

PHP Replace values in an array into an HTML string

Sounds simple enough, but I'm feeling quite stupid today.

If I have an array like this:

$defined_vars = array(
    '{POST_TITLE}' => $item['post']['name'], 
    '{POST_LINK}' => $item['post']['link'], 
    '{TOPIC_TITLE}' => $item['topic']['name'], 
    '{TOPIC_LINK}' => $item['topic']['link'], 
    '{MEMBERNAME}' => $txt['by'] . ' <strong>' . $item['membername'] . '</strong>', 
    '{POST_TIME}' => $item['time'], 
    '{VIEWS}' => $txt['attach_viewed'] . ' ' . $item['file']['downloads'] . ' ' . $txt['attach_times'],
    '{FILENAME}' => $item['file']['name'],
    '{FILENAME_LINK}' => '<a href="' . $item['file']['href'] . '">' . $item['file']['name'] . '</a>',
    '{FILESIZE}' => $item['file']['size'],
    '{DIMENSIONS}' => $item['file']['image']['width'] 'x' $item['file']['image']['height'],
);

And a String like this:

$string = '<div class="largetext centertext">{POST_LINK}</div><div class="smalltext centertext">{MEMBERNAME}</div><div class="floatright smalltext dp_paddingright">{POST_TIME}</div><div class="dp_paddingleft smalltext">{VIEWS}</div>';

I need it to be replaced with the values of those keys. Is that possible to do? Perhaps using str_replace() somehow? Are array keys allowed to have curly brackets within them? Would that cause any problems? Also, I need this to replace the $string value for ALL times these are found, cause it is possible to have the same output desired more than 1 time. For example, if {POST_TITLE} is defined twice it should output the value twice exactly where they have used it at within the string.

Thanks

Upvotes: 1

Views: 1620

Answers (4)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

str_replace supports array. The following syntax will do it.

$string=str_replace(array_keys($defined_vars), array_values($defined_vars), $string);

curly braces are supported in array keys because it's in string and strings are supported as array yes.

Upvotes: 4

hackartist
hackartist

Reputation: 5264

foreach($defined_vars as $key=>$value) {
  $string = str_replace($key,$value,$string);
}

this is using str_replace like you asked and it is easy to see what is happening. Php also has the strtr or string translate function to do pretty much just this so you could also use

$string = strtr($string,$defined_vars);

but would have to remember what that function does.

Upvotes: 3

Your Common Sense
Your Common Sense

Reputation: 157870

<div class="largetext centertext">
  <a href="<?=$item['post']['link']?>"><?=$item['post']['title']?></a>
</div>
<div class="smalltext centertext">
  <?=$txt['by']?><strong><?$item['membername']?></strong>
</div>
<div class="floatright smalltext dp_paddingright"><?$item['time']?></div>
<div class="dp_paddingleft smalltext">
  <?=$txt['attach_viewed']?>
  <?=$item['file']['downloads']?>
  <?=$txt['attach_times']?>
</div>

well, if it's a user-defined string, you have to replace

$string = strtr($string,$defined_vars);

Also I hope you filter out user-edited HTML, to prevent them from stealing your cookies and login as admin or any other user.

Upvotes: 0

Scuzzy
Scuzzy

Reputation: 12332

Yep, your str_replace is suitable, just foreach() loop your array

if(isset($defined_vars) and is_array($defined_vars))
{
  foreach($defined_vars as $token => $replacement)
  {
    $string = str_replace($token,$replacement,$string);
  }
}

you may want to apply some filters to your variables to ensure you don't have your HTML wrecked.

Upvotes: 0

Related Questions