function str_replace_formatted_placeholder($mixedplaceholder, $mixedreplace, $subject, $question_mark = false, $separator = ", ") { # Returns a string with all occurrences of the $mixedplaceholder in $subject replaced with the $mixedreplace. If $mixedplaceholder is a string but $mixedreplace is an array, the $mixedreplace is imploded to a string using $separator. # The replace values are formatted according to the formatting of the placeholders. # The placeholders may be written in UPPERCASE, lowercase or Uppercasefirst. # Each placeholder will be replaced by the replace value, # written with the same case as the placeholder. # It's possible to also include "?" as a placeholder for legacy reasons. # Example #1: # str_replace_formatted_placeholder("%extension", $resource["file_extension"], $lang["originalfileoftype"], true) # will search for the three words "%EXTENSION", "%extension" and "%Extension" and also the char "?" # in the string $lang["originalfileoftype"]. If the found placeholder is %extension # it will be replaced by the value of $resource["file_extension"], # written in lowercase. If the found placeholder instead would have been "?" the value # would have been written in UPPERCASE. # # Example #2: # str_replace_formatted_placeholder("%resourcetypes%", $searched_resource_types_names_array, $lang["resourcetypes-collections"], false, $lang["resourcetypes_separator"]) # will search for the three words "%RESOURCETYPES%", "%resourcetypes%" and "%Resourcetypes%" # in the string $lang["resourcetypes-collections"]. If the found placeholder is %resourcetypes% # all elements in $searched_resource_types_names_array will be written in lowercase and separated by $lang["resourcetypes_separator"] before the resulting string will replace the placeholder. # Creates a multi-dimensional array of the placeholders written in different case styles. $array_placeholder = array(); if (is_array($mixedplaceholder)) { $placeholder = $mixedplaceholder; } else { $placeholder = array($mixedplaceholder); } for ($n = 0; $n < count($placeholder); $n++) { $array_placeholder[$n] = array(strtoupper($placeholder[$n]), strtolower($placeholder[$n]), ucfirstletter($placeholder[$n])); } # Creates a multi-dimensional array of the replace values written in different case styles. if (is_array($mixedreplace)) { $replace = $mixedreplace; } else { $replace = array($mixedreplace); } for ($n = 0; $n < count($replace); $n++) { $array_replace[$n] = array(strtoupper($replace[$n]), strtolower($replace[$n]), ucfirst(strtolower($replace[$n]))); } # Adds "?" to the arrays if required. if ($question_mark) { $array_placeholder[] = "?"; $array_replace[] = strtoupper($replace[0]); } # Replaces the placeholders with the replace values and returns the new string. $result = $subject; if (count($placeholder) == 1 && count($replace) > 1) { # The placeholder shall be replaced by an imploded array. $array_replace_strings = array(implode($separator, array_map(create_function('$column', 'return $column[0];'), $array_replace)), implode($separator, array_map(create_function('$column', 'return $column[1];'), $array_replace)), implode($separator, array_map(create_function('$column', 'return $column[2];'), $array_replace))); $result = str_replace($array_placeholder[0], $array_replace_strings, $result); } else { for ($n = 0; $n < count($placeholder); $n++) { if (!isset($array_replace[$n][0])) { break; } else { $result = str_replace($array_placeholder[$n], $array_replace[$n], $result); } } } return $result; }
function str_replace_formatted_placeholder($mixedplaceholder, $mixedreplace, $subject, $question_mark = false) { # Returns a string with all occurrences of the placeholders (array) in subject replaced with the given replace values (array). The replace values are formatted according to the formatting of the placeholders. # The placeholders may be written in UPPERCASE, lowercase or Uppercasefirst. # Each placeholder will be replaced by the replace value, # written with the same case as the placeholder. # It's possible to also include "?" as a placeholder for legacy reasons. # E.g. # str_replace_formatted_placeholder("%extension", $resource["file_extension"], $lang["originalfileoftype"], true) # will search for the three words "%EXTENSION", "%extension" and "%Extension" and also the char "?" # in the string $lang["originalfileoftype"]. If the found placeholder is %extension # it will be replaced by the value of $resource["file_extension"], # written in lowercase. If the found placeholder instead would have been "?" the value # would have been written in UPPERCASE. # Creates a multi-dimensional array of the placeholders written in different case styles. if (is_array($mixedplaceholder)) { $placeholder = $mixedplaceholder; } else { $placeholder = array($mixedplaceholder); } for ($n = 0; $n < count($placeholder); $n++) { $array_placeholder[$n] = array(strtoupper($placeholder[$n]), strtolower($placeholder[$n]), ucfirstletter($placeholder[$n])); } # Creates a multi-dimensional array of the replace values written in different case styles. if (is_array($mixedreplace)) { $replace = $mixedreplace; } else { $replace = array($mixedreplace); } for ($n = 0; $n < count($replace); $n++) { $array_replace[$n] = array(strtoupper($replace[$n]), strtolower($replace[$n]), ucfirst($replace[$n])); } # Adds "?" to the arrays if required. if ($question_mark) { $array_placeholder[] = "?"; $array_replace[] = strtoupper($replace[0]); } # Replaces the placeholders with the replace values and returns the new string. $result = $subject; for ($n = 0; $n < count($placeholder); $n++) { if (!isset($array_replace[$n][0])) { break; } else { $result = str_replace($array_placeholder[$n], $array_replace[$n], $result); } } return $result; }