Return to Snippet

Revision: 63965
at July 1, 2013 09:54 by jrobinsonc


Updated Code
<?php

/**
 * Cut text to specific length.
 *
 * @param string $str The text to cut.
 * @param int $limit The maximum number of characters that must be returned.
 * @param stirng $br_char The character to use for breaking the string.
 * @param string $pad The string to use at the end of the cutted string.
 * @return string
 */
function cut_text($str, $limit, $br_char = '', $pad = '...')
{
    if (strlen($str) <= $limit) return $str;

    if ($br_char === '') return substr($str, 0, $limit) . $pad;

    return substr($str, 0, strrpos(substr($str, 0, $limit), $br_char)) . $pad;
}

$text1 = 'Lorem ipsum dolor sit amet, sed do eiusmod tempor incididunt ut laboredo.';

// Normal usage:
printf('%s<br>', cut_text($text1, 20)); 

// Using an space for break the string:
printf('%s<br>', cut_text($text1, 20, ' '));

Revision: 63964
at July 1, 2013 09:30 by jrobinsonc


Updated Code
<?php

function cut_text($str, $limit, $pad = '...', $br_char = '')
{
    if (strlen($str) <= $limit) return $str;

    if ($br_char === '') return substr($str, 0, $limit) . $pad;

    return substr($str, 0, strrpos(substr($str, 0, $limit), $br_char)) . $pad;
}
 
 
// Example:
$text1 = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore .';

echo cut_text($text1, 20, '...', ' ');

Revision: 63963
at July 1, 2013 09:28 by jrobinsonc


Updated Code
<?php

function cut_text($str, $limit, $pad = '...', $br_char = '')
{
    if (strlen($str) <= $limit) return $str;

    if ($br_char === '') return substr($str, 0, $limit) . $pad;

    return substr($str, 0, strrpos(substr($str, 0, $limit), $br_char)) . $pad;
}
 
 
// Ejemplo de uso:
$text1 = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore .';
 
echo cut_text($text1, 20);

Revision: 63962
at June 22, 2013 02:35 by jrobinsonc


Initial Code
<?php

function cut_text($str, $limit, $end = '...')
{
    return substr($str, 0, strpos($str, ' ', $limit)) . $end;
}
 
 
// Ejemplo de uso:
$text1 = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore .';
 
echo cut_text($text1, 20);

Initial URL


Initial Description
Cut text to specific length.

Initial Title
Cut text to specific length.

Initial Tags


Initial Language
PHP