$value) { $placeholder = $delimiters[0] . $name . $delimiters[1]; if (empty($value)) { $string = preg_replace('/\[[^\[]*' . preg_quote($placeholder) . '[^\]]*\]/', '', $string); } } foreach ($placeholders as $name => $value) { $placeholder = $delimiters[0] . $name . $delimiters[1]; // remove optional marks for nonempty placeholder, e.g: "[{prefix}Title: {title}]" to "{prefix}Title: {title}" if (!empty($value)) { $string = preg_replace('/(\[)([^\[]*' . preg_quote($placeholder) . '[^\]]*)(\])/', '$2', $string); } $string = str_replace($placeholder, $value, $string); } return $string; } }