Jump to content


Photo

Funçao Parecida Com A Truncate Do Smarty


  • Faça o login para participar
1 reply to this topic

#1 helio_ceara

helio_ceara

    Turista

  • Usuários
  • 43 posts
  • Sexo:Não informado

Posted 23/12/2005, 15:41

alguem sbae alguma função que seja do php que realize a mesma função do truncate do smarty, ou seja, a função recebe uma string e um valor inteiro(50 por exemplo) e a funçao retorna os primeiros 50 caracteres da string!
vlw ae!
Se puder ajudar Agradesso!
Helio Viana

Edição feita por: helio_ceara, 23/12/2005, 15:43.


#2 Paulo Freitas

Paulo Freitas

    ××××××× LRU #456504 ××××××× ××××××× LRM #364686 ×××××××

  • Ex-Admins
  • 5612 posts
  • Sexo:Masculino
  • Localidade:Campinas - SP

Posted 23/12/2005, 16:09

Sabia que o Smarty possui código-fonte ? :P

Eis o fonte do modificador truncate:
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty truncate modifier plugin
 *
 * Type:     modifier<br>
 * Name:     truncate<br>
 * Purpose:  Truncate a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
 *          truncate (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                  $break_words = false, $middle = false)
{
    if ($length == 0)
        return '';

    if (strlen($string) > $length) {
        $length -= strlen($etc);
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
        }
        if(!$middle) {
            return substr($string, 0, $length).$etc;
        } else {
            return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
        }
    } else {
        return $string;
    }
}

/* vim: set expandtab: */

?>
[]’s :DAté mais




0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq