Common methods for intercepting strings in PHP
- /**
- * Truncate a string
- * Params $ string the string to be intercepted
- * Params $ length: reserved length (number of characters)
- * Params $ dot: Display excess parts
- **/
- Function _ cutstr ($ string, $ length, $ dot = '...'){
- If (strlen ($ string) <= $ length ){
- Return $ string;
- }
- $ String = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ string );
- $ Strcut = '';
- $ N = $ tn = $ noc = 0;
- While ($ n <strlen ($ string )){
- $ T = ord ($ string [$ n]);
- If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
- $ Tn = 1; $ n ++; $ noc ++;
- } Elseif (194 <=$ t & $ t <= 223 ){
- $ Tn = 2; $ n + = 2; $ noc + = 2;
- } Elseif (224 <=$ t & $ t <239 ){
- $ Tn = 3; $ n + = 3; $ noc + = 2;
- } Elseif (240 <=$ t & $ t <= 247 ){
- $ Tn = 4; $ n + = 4; $ noc + = 2;
- } Elseif (248 <=$ t & $ t <= 251 ){
- $ Tn = 5; $ n + = 5; $ noc + = 2;
- } Elseif ($ t = 252 | $ t = 253 ){
- $ Tn = 6; $ n + = 6; $ noc + = 2;
- } Else {
- $ N ++;
- }
- If ($ noc >=$ length ){
- Break;
- }
- }
- If ($ noc> $ length ){
- $ N-= $ tn;
- }
- $ Strcut = substr ($ string, 0, $ n );
- $ Strcut = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ strcut );
- Return $ strcut. $ dot;
- }
|
PHP