*/ /** * patTemplate modifier Explode * * Explode using delimiter and return part of array as index * * @package patTemplate * @package Modifiers * @author gERD Schaufelberger */ class patTemplate_Modifier_Explode extends patTemplate_Modifier { /** * Explode * * @param string value * @param array $params * @return string modified value */ public function modify( $value, $params = array() ) { if (empty($value)) { return $value; } if (!is_array($params)) { $params = array(); } $default = array( 'delimiter' => '/', 'index' => 0, 'glue' => '', 'format' => '%s' ); $params = array_merge($default, $params); // nothing to do if (empty($params['delimiter'])) { return $value; } // explode if ("\\n" == $params['delimiter']) { $params['delimiter'] = "\n"; } $value = explode($params['delimiter'], $value); // format each line if ('%s' != $params['format']) { foreach ($value as &$v) { $v = sprintf($params['format'], $v); } } // glue if (!empty($params['glue'])) { if ("\\n" == $params['glue']) { $params['glue'] = "\n"; } $value = implode($params['glue'], $value); return $value; } // use index $value = $value[$params['index']]; return $value; } }