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 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. 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; } 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; } 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; } ?> 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 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
Archived
This topic is now archived and is closed to further replies.