Created
December 7, 2012 03:45
-
-
Save Homlean/4230608 to your computer and use it in GitHub Desktop.
php string 截取
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 字符截取 | |
// Truncate ? | |
function strCut($sourceStr, $cutLength) { | |
$result = ''; | |
$i = 0; | |
$n = 0; | |
$sourceStrLen = strlen($sourceStr); | |
while (($n < $cutLength) and ($i <= $sourceStrLen)) { | |
$temp = substr($sourceStr, $i, 1); | |
$asciiCode = ord($temp); | |
if ($asciiCode >= 224) { | |
$result = $result . substr($sourceStr, $i, 3); | |
$i = $i + 3; | |
$n++; | |
} elseif ($asciiCode >= 192) { | |
$result = $result . substr($sourceStr, $i, 2); | |
$i=$i+2; | |
$n++; | |
} elseif ($asciiCode >= 65 && $asciiCode <= 90) { | |
$result = $result . substr($sourceStr, $i, 1); | |
$i = $i + 1; | |
$n++; | |
} else { | |
$result = $result . substr($sourceStr, $i, 1); | |
$i = $i + 1; | |
$n = $n + 0.5; | |
} | |
} | |
if ($sourceStrLen > $cutLength) { | |
$result = $result . '...'; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment