Skip to content

Instantly share code, notes, and snippets.

@orymate
Created May 17, 2010 13:52
Show Gist options
  • Save orymate/403775 to your computer and use it in GitHub Desktop.
Save orymate/403775 to your computer and use it in GitHub Desktop.
<?php
function utfcrop($string, $bytecount)
{
$ret = "";
$len = strlen($string);
for ($i = 0; $i < $len; $i++) {
if (($string[$i] & 0x80) == 0 && $i + 1 > $bytecount)
return $ret;
else if (($string[$i] & 0xE0) == 0xD0 && $i + 2 > $bytecount)
return $ret;
else if (($string[$i] & 0xF0) == 0xE0 && $i + 3 > $bytecount)
return $ret;
else if (($string[$i] & 0xF8) == 0xF0 && $i + 4 > $bytecount)
return $ret;
else
$ret .= $string[$i];
}
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment