CakeFest 2025 Madrid: The Official CakePHP Conference

Voting

: four minus one?
(Example: nine)

The Note You're Voting On

david at ethinkn dot com
21 years ago
Here is a simple function to shorten a string and add an ellipsis

<?php

/**
* truncate() Simple function to shorten a string and add an ellipsis
*
* @param string $string Origonal string
* @param integer $max Maximum length
* @param string $rep Replace with... (Default = '' - No elipsis -)
* @return string
* @author David Duong
**/
function truncate ($string, $max = 50, $rep = '') {
$leave = $max - strlen ($rep);
return
substr_replace($string, $rep, $leave);
}

echo
truncate ('akfhslakdhglksjdgh', 10, '...');
// Returns akfhsla... (10 chrs)

?>

<< Back to user notes page

To Top