actually you can do this very easily like this:
<?php
function smart_trim($string,$cutoff,$character) {
$cutoff = intval($cutoff);
$retval = substr($string,0,(stripos($string,$character,$cutoff);
return $retval;
} //end smart_trim
?>
then call it like this:
<?php
$my_string = "This function is so easy to use, I love this function with all my heart";
$print_string = smart_trim($my_string,21,' ');
echo $print_string;
?>
This prints: This function is so easy