Exemplo n.º 1
0
/**
 * Modifier plugin : count the number of characters in a text.
 *
 * <pre>{$mytext|count_characters}
 * {$mytext|count_characters:true}</pre>
 *
 * @param \Jelix\Castor\CastorCore $tpl The template
 * @param string $string
 * @param bool $include_spaces include whitespace in the character count
 *
 * @return int
 */
function jtpl_modifier2_common_count_characters(\Jelix\Castor\CastorCore $tpl, $string, $include_spaces = false)
{
    if ($include_spaces) {
        return iconv_strlen($string, $tpl->getEncoding());
    }
    return preg_match_all("/[^\\s]/", $string, $match);
}
Exemplo n.º 2
0
/**
 * modifier plugin :  Truncate a string.
 *
 * Truncate a string to a certain length if necessary, optionally splitting in
 * the middle of a word, and appending the $etc string.
 * <pre>{$mytext|truncate}
 * {$mytext|truncate:40}
 * {$mytext|truncate:45:'...'}
 * {$mytext|truncate:60:'...':true}
 * </pre>
 *
 * @param \Jelix\Castor\CastorCore $tpl The template
 * @param string $string the string to truncate
 * @param int $length the number of char to keep
 * @param string $etc the string to append to the truncated string
 * @param bool $break_words false if the last word shouldn't be cut
 *
 * @return string
 */
function jtpl_modifier2_common_truncate(\Jelix\Castor\CastorCore $tpl, $string, $length = 80, $etc = '...', $break_words = false)
{
    if (function_exists('mb_strlen')) {
        $f_strlen = 'mb_strlen';
    } else {
        $f_strlen = 'iconv_strlen';
    }
    if (function_exists('mb_substr')) {
        $f_substr = 'mb_substr';
    } else {
        $f_substr = 'iconv_substr';
    }
    if ($length == 0) {
        return '';
    }
    $charset = $tpl->getEncoding();
    if ($f_strlen($string, $charset) > $length) {
        $length -= $f_strlen($etc, $charset);
        if (!$break_words) {
            $string = preg_replace('/\\s+?(\\S+)?$/', '', $f_substr($string, 0, $length + 1, $charset));
        }
        return $f_substr($string, 0, $length, $charset) . $etc;
    } else {
        return $string;
    }
}
Exemplo n.º 3
0
/**
 * modifier plugin : regular epxression search/replace.
 *
 * You should provide two arguments, like the first both of preg_replace
 * {$mystring|regex_replace:'/(\w+) (\d+), (\d+)/i':'${1}1,$3'}
 *
 * @param \Jelix\Castor\CastorCore $tpl The template
 * @param string $string
 * @param string|array $search
 * @param string|array $replace
 *
 * @return string
 */
function jtpl_modifier2_common_regex_replace(\Jelix\Castor\CastorCore $tpl, $string, $search, $replace)
{
    if (preg_match('!\\W(\\w+)$!s', $search, $match) && strpos($match[1], 'e') !== false) {
        /* remove eval-modifier from $search */
        $search = substr($search, 0, -iconv_strlen($match[1], $tpl->getEncoding())) . str_replace('e', '', $match[1]);
    }
    return preg_replace($search, $replace, $string);
}