PHP Chinese intercept function

Source: Internet
Author: User

<?php
/*
* Chinese characters interception
* @param string $string the strings being processed
* @param int $start Where to start the Intercept
* @param int $length The length of the truncated character
* @param string $charset character encoding
* @param string $dot thumbnail symbol
*
* @return String
*/function cutstr ($string, $start, $length, $charset = "Utf-8", $dot = ") {

if (function_exists ("Mb_substr")) {
if (Mb_strlen ($string, $charset) > $length) {
Return Mb_substr ($string, $start, $length, $charset). $dot;
}
Return Mb_substr ($string, $start, $length, $charset);
}
$new = ";
if ($charset = = = ' GBK ') {
$string = Iconv ("GBK", "Utf-8", $string);
}
$str = Preg_split ('//u ', trim ($string));
for ($i = $start, $len = 1; $i <count ($STR)-1 && $len <= $length; $i + +, $len + +) {
$new. = $str [$i +1];
}
if ($charset = = = ' GBK ') {
$new = Iconv ("Utf-8", "GBK", $new);
}
return count ($str) -2< $length? $new: $new. $dot;
}
echo cutstr (' dark tones adsa dark tones ', 0,1, ' utf-8 ', ' ... ');
?>

PHP Chinese intercept function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.