Function cut_str ($ sourcestr, $ cutlength ){
$ Returnstr = '';
$ I = 0;
$ N = 0;
$ Str_length = strlen ($ sourcestr); // number of bytes of the string
While ($ n <$ cutlength) and ($ I <= $ str_length ))
{
$ Temp_str = substr ($ sourcestr, $ I, 1 );
$ Ascnum = ord ($ temp_str); // obtain the ASCII code of the $ I character in the string
If ($ ascnum> = 224) // If the ASCII bit height is 224,
{
// According to the UTF-8 encoding specification, count 3 consecutive characters as a single character
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 3 );
$ I = $ I + 3; // The actual byte count is 3.
$ N ++; // string length Meter 1
}
Elseif ($ ascnum> = 192) // If the ASCII bit height is 192,
{
// According to the UTF-8 encoding specification, Count 2 consecutive characters as a single character
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 2 );
$ I = $ I + 2; // The actual byte count is 2.
$ N ++; // string length Meter 1
}
Elseif ($ ascnum >=65 & $ ascnum <= 90) // if it is a capital letter,
{
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 1 );
$ I = $ I + 1; // the actual number of bytes is still counted as 1
$ N ++; // consider the overall appearance. uppercase letters are counted as a high character.
}
Else // in other cases, including lower-case letters and halfwidth punctuation marks,
{
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 1 );
$ I = $ I + 1; // the actual number of bytes is 1.
$ N = $ n + 0.5; // lower-case letters and halfwidth punctuation and half-height character width...
}
}
If ($ str_length> $ cutlength ){
$ Returnstr = $ returnstr. "..."; // when the length is exceeded, add a ellipsis at the end.
}
Return $ returnstr;
}