1

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

4 Answers 4

4

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.

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

2 Comments

+1 I always forget about using array_keys/array_values this way
@Scuzzy happens to me too :-)
3
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.

2 Comments

Why the downvote? This is the simplest way to do it that will work
Wow, thanks so much for strtr function. Didn't know that this was possible. Marked this as my answer because strtr is faster, so it seems, than using str_replace with array_keys and array_values! Thanks so much for this! :)
0

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.

3 Comments

What do you mean by filters to the variables to ensure that the HTML is not wrecked?? And thanks btw :)
Why are you downvoting all the answers?
@SoLoGHoST eg if you need to escape any html to ensure clean output, or have you done that ahead of time?
0
<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.

2 Comments

Huh? I need to replace {POST_TIME} within a string with the value of $defined_vars['{POST_TIME}']; and so on, for any other ones that are within the string. This is a user-defined string, so it can hold any of the $defined_vars keys. The HTML, the user can change at any time via a textbox...
No need to bark. Polite explanation would be enough. for such a limited case here is another solution

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.