Use Perl to intercept a string that contains Chinese characters, English letters, numbers, and symbols. It also contains Chinese Punctuation Marks. After intercept a certain number of characters, the Chinese character is complete, no garbled characters or question marks will appear.
Sub makelen ($)
{
My ($ SSTR, $ slen) = @_;
My $ makelen, $ parity;
$ Mslen = length ($ SSTR );
If ($ mslen> $ slen)
{
For ($ Mn = 0; $ Mn <$ slen; $ Mn ++) # within the required length
{
$ Ss = substr ($ SSTR, $ Mn, 1 );
If (ord ($ SS) <127) # English
{
$ Parity + = 1;
}
}
If ($ parity % 2 = 1) # contains an odd number of English characters
{
If ($ slen % 2 = 1) # If the required length is also odd
{
$ Tmpstr = substr ($ SSTR, 0, $ slen );
}
Else # The required length is an even number.
{
$ Tmpstr = substr ($ SSTR, 0, $ slen-1 );
}
}
Else # contains an even number of English characters or an even number of characters ranging from 0 to 0.
{
If ($ slen % 2 = 1) # If the required length is odd
{
$ Tmpstr = substr ($ SSTR, 0, $ slen-1 );
}
Else # The required length is an even number.
{
$ Tmpstr = substr ($ SSTR, 0, $ slen );
}
}
$ Makelen = $ tmpstr;
}
Else
{
$ Makelen = $ SSTR;
}
Return $ makelen;
}