doddsey_65 Posted January 26, 2011 Share Posted January 26, 2011 hi, im trying to replace an arrays key with its value. this is so i can display the html on the pages. eg: index.html <title>{BOARD_TITLE}</title> index.php $template->replace(array( '{BOARD_TITLE}' => $settings['board_name'] )); template.php(template class) function replace($array) { foreach ($array as $key => $val) { echo $key = $val; } return; } however {BOARD_TITLE} does not get replaced with the value. where am i going wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/225688-replace-array-key-with-val/ Share on other sites More sharing options...
doddsey_65 Posted January 26, 2011 Author Share Posted January 26, 2011 ive gotten one step closer but its only replacing the first part of the array. function replace($array, $file) { $file = 'html/'.$file.'.html'; foreach ($array as $key => $val) { echo str_replace($key, $val, file_get_contents('./'.$this->template.$file)); } return; } $template->replace(array( '{BOARD_NAME}' => $settings['board_short_name'], '{PAGE_TITLE}' => $lang->index ), 'header'); {PAGE_TITLE} does not get replaced. Quote Link to comment https://forums.phpfreaks.com/topic/225688-replace-array-key-with-val/#findComment-1165309 Share on other sites More sharing options...
PinoyProgrammer Posted January 26, 2011 Share Posted January 26, 2011 You can use array_flip() for that. If somehow you don't need that. How about this: function replace ($array) { foreach ($array as $key => $val) { $array[$val] = $key; $array[$key] = NULL; } return $array; } Quote Link to comment https://forums.phpfreaks.com/topic/225688-replace-array-key-with-val/#findComment-1165313 Share on other sites More sharing options...
doddsey_65 Posted January 26, 2011 Author Share Posted January 26, 2011 that doesnt seem to work since the html file isnt included anywhere. ive tried again, thinking it was a loop i needed but now {BOARD_NAME} isnt even replaced: function replace($array, $file) { if(!is_array($array)) { die('template->replace: No Array Specified'); } $file = 'html/'.$file.'.html'; $replace = ''; foreach ($array as $key => $val) { while($element = each($array)) { $replace .= str_replace($element['key'], $element['val'], file_get_contents('./'.$this->template.$file)); } } echo $replace; } Quote Link to comment https://forums.phpfreaks.com/topic/225688-replace-array-key-with-val/#findComment-1165345 Share on other sites More sharing options...
sasa Posted January 26, 2011 Share Posted January 26, 2011 try <?php function replace($array, $file) { $file = 'html/'.$file.'.html'; $file = file_get_contents('./'.$this->template.$file); foreach ($array as $key => $val) $file = str_replace($key, $val, $file); echo $file; return; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225688-replace-array-key-with-val/#findComment-1165352 Share on other sites More sharing options...
doddsey_65 Posted January 26, 2011 Author Share Posted January 26, 2011 cheers mate, youre a star. works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/225688-replace-array-key-with-val/#findComment-1165353 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.