function utf8_strtrunc($str, $len) {
		$i = $len - 1;
		if (ord($str{$i}) & 0x80) { // part of a multibyte sequence
			for (; $i > 0 && !(ord($str{$i}) & 0x40); $i--) {
				// find the first byte
				;
			}
			if ($i <= 0) {
				return '';
			} // sanity check
			for ($bc = 0, $mbs = ord($str{$i}); $mbs & 0x80; $mbs = $mbs << 1) {
				// calculate number of bytes
				$bc++;
			}
			if ($bc + $i > $len) {
				return substr($str, 0, $i);
			}
			// fallthru: multibyte char fits into length
		}
		return substr($str, 0, $len);
	}
function utf8_strtrunc($str, $len) {
		$i = $len - 1;
		if (ord($str{$i}) & 0x80) { // part of a multibyte sequence
			for (; $i > 0 && !(ord($str{$i}) & 0x40); $i--) {
				// find the first byte
				;
			}
			if ($i <= 0) {
				return '';
			} // sanity check
			for ($bc = 0, $mbs = ord($str{$i}); $mbs & 0x80; $mbs = $mbs << 1) {
				// calculate number of bytes
				$bc++;
			}
			if ($bc + $i > $len) {
				return substr($str, 0, $i);
			}
			// fallthru: multibyte char fits into length
		}
		return substr($str, 0, $len);
	}
function utf8_strtrunc ( $ str , $ len ) { $ i = $ len - 1 ; if ( ord ( $ str { $ i } ) & 0x80 ) { // part of a multibyte sequence for ( ; $ i > 0 && ! ( ord ( $ str { $ i } ) & 0x40 ) ; $ i -- ) { // find the first byte ; } if ( $ i <= 0 ) { return '' ; } // sanity check for ( $ bc = 0 , $ mbs = ord ( $ str { $ i } ) ; $ mbs & 0x80 ; $ mbs = $ mbs << 1 ) { // calculate number of bytes $ bc ++ ; } if ( $ bc + $ i > $ len ) { return substr ( $ str , 0 , $ i ) ; } // fallthru: multibyte char fits into length } return substr ( $ str , 0 , $ len ) ; }
Truncates a string in UTF-8 short at a given byte length.

@param	string		UTF-8 multibyte character string
@param	integer		the byte length
@return	string		the shortened string
@see mb_strcut()
@author	Martin Kutschker <martin.t.kutschker@blackbox.net>
Truncates a string in UTF - 8 short at a given byte length .