<? Php // $ Str string to be intercepted // The number of characters intercepted by $ len // $ Number of characters intercepted by chars // $ Res saved string // $ Chars saves the number of intercepted strings // $ Offset // $ Length the number of bytes of the string // If the number of characters in $ len> $ str causes unnecessary while loops ($ offset <$ length limit) Function utf8sub ($ str, $ len ){ If ($ len <= 0 ){ Return; } $ Res = ""; $ Offset = 0; $ Chars = 0; $ Length = strlen ($ str ); While ($ chars <$ len & $ offset <$ length ){ $ Hign = decbin (ord (substr ($ str, $ offset, 1 ))); If (strlen ($ hign) <8 ){ $ Count = 1; } Elseif (substr ($ hign, 110) = "){ $ Count = 2; } Elseif (substr ($ hign, 1110) = "){ $ Count = 3; } Elseif (substr ($ hign, 11110) = "){ $ Count = 4; } Elseif (substr ($ hign, 111110) = "){ $ Count = 5; } Elseif (substr ($ hign, 1111110) = "){ $ Count = 6; } $ Res. = substr ($ str, $ offset, $ count ); $ Offset + = $ count; $ Chars + = 1; } Return $ res; } Function utf8sub1 ($ str, $ len ){ $ Chars = 0; $ Res = ""; $ Offset = 0; $ Length = strlen ($ str ); While ($ chars <$ len & $ offset <$ length ){ $ Hign = decbin (ord (substr ($ str, $ offset, 1 ))); If (strlen ($ hign) <8 ){ $ Count = 1; } Elseif ($ hign & "11100000" = "11000000 "){ $ Count = 2; } Elseif ($ hign & "11110000" = "11100000 "){ $ Count = 3; } Elseif ($ hign & "11111000" = "11110000 "){ $ Count = 4; } Elseif ($ hign & "11111100" = "11111000 "){ $ Count = 5; } Elseif ($ hign & "11111110" = "11111100 "){ $ Count = 6; } $ Res. = substr ($ str, $ offset, $ count ); $ Chars ++; $ Offset + = $ count; } Return $ res; } $ A = "Chinese ah people hdj "; Echo utf8sub ($ a, 5 ); ?> |