首页 > 开发 > PHP > 正文

PHP程序字符串处理函数

2024-05-04 21:52:55
字体:大 中 小
来源:转载
供稿:网友
  1. <?  
  2. //------------------------  
  3. // PHP内置字符串函数实现 
  4. //------------------------ 
  5.  
  6. //字符串长度  
  7. function strlen (str)  
  8. {  
  9. if (str == '' ) return 0;  
  10. count = 0;  
  11. while (1){  
  12. if ( str[count] != NULL){  
  13. count++;  
  14. continue;  
  15. }else{  
  16. break;  
  17. }  
  18. }  
  19. return count;  
  20. } 
  21.  
  22. //截取子串  
  23. function substr(str, start, length=NULL)  
  24. {  
  25. if (str== '' || start>strlen(str )) return;  
  26. if ((length!=NULL) && ( start>0) && (length> strlen(str)-start)) return;  
  27. if (( length!=NULL) && (start< 0) && (length>strlen(str )+start)) return;  
  28. if (length == NULL) length = (strlen(str ) - start);  
  29.  
  30. if (start < 0){  
  31. for (i=(strlen( str)+start); i<(strlen (str)+start+length ); i++) {  
  32. substr .= str[i];  
  33. }  
  34. }  
  35. if (length > 0){  
  36. for (i= start; i<(start+length ); i++) {  
  37. substr .= str[i];  
  38. }  
  39. }  
  40. if ( length < 0){  
  41. for (i =start; i<(strlen( str)+length); i++) {  
  42. substr .= str[i ];  
  43. }  
  44. }  
  45. return substr;  
  46. } 
  47.  
  48. //字符串翻转  
  49. function strrev(str)  
  50. {  
  51. if (str == '') return 0 ;  
  52. for (i=(strlen(str)- 1); i>=0; i --){  
  53. rev_str .= str[i ];  
  54. }  
  55. return rev_str;  
  56. } 
  57.  
  58. //字符串比较  
  59. function strcmp(s1, s2)  
  60. {  
  61. if (strlen(s1) < strlen(s2)) return -1 ;  
  62. if (strlen(s1) > strlen( s2)) return 1;  
  63. for (i =0; i<strlen(s1 ); i++){  
  64. if (s1[ i] == s2[i]){  
  65. continue;  
  66. }else{  
  67. return false;  
  68. }  
  69. }  
  70. return 0;  
  71. } 
  72.  
  73. //查找字符串  
  74. function strstr(str, substr)  
  75. {  
  76. m = strlen(str);  
  77. n = strlen(substr );  
  78. if (m < n) return false ;  
  79. for (i=0; i <=(m-n+1); i ++){  
  80. sub = substr( str, i, n);  
  81. if ( strcmp(sub, substr) == 0) return i;  
  82. }  
  83. return false ;  
  84. } 
  85.  
  86. //字符串替换  
  87. function str_replace(substr , newsubstr, str)  
  88. {  
  89. m = strlen(str);  
  90. n = strlen(substr );  
  91. x = strlen(newsubstr );  
  92. if (strchr(str, substr ) == false) return false;  
  93. for ( i=0; i<=(m- n+1); i++){  
  94. i = strchr(str, substr);  
  95. str = str_delete (str, i, n);  
  96. str = str_insert(str, i, newstr);  
  97. }  
  98. return str ;  
  99. }  
  100. ?> 
  101.  
  102. <?php 
  103. //--------------------  
  104. // 自实现字符串处理函数 
  105. //-------------------- 
  106.  
  107. //插入一段字符串  
  108. function str_insert(str, i , substr)  
  109. {  
  110. for(j=0 ; j<i; j ++){  
  111. startstr .= str[j ];  
  112. }  
  113. for (j=i; j <strlen(str); j ++){  
  114. laststr .= str[j ];  
  115. }  
  116. str = (startstr . substr . laststr);  
  117. return str ;  
  118. } 
  119.  
  120. //删除一段字符串  
  121. function str_delete(str , i, j)  
  122. {  
  123. for ( c=0; c<i; c++){  
  124. startstr .= str [c];  
  125. }  
  126. for (c=( i+j); c<strlen (str); c++){  
  127. laststr .= str[c];  
  128. }  
  129. str = (startstr . laststr );  
  130. return str;  
  131. } 
  132.  
  133. //复制字符串  
  134. function strcpy(s1, s2 )  
  135. {  
  136. if (strlen(s1)==NULL || !isset( s2)) return;  
  137. for (i=0 ; i<strlen(s1); i++){  
  138. s2[] = s1 [i];  
  139. }  
  140. return s2;  
  141. } 
  142.  
  143. //连接字符串  
  144. function strcat(s1 , s2)  
  145. {  
  146. if (!isset(s1) || !isset( s2)) return;  
  147. newstr = s1 ;  
  148. for(i=0; i <count(s); i ++){  
  149. newstr .= st[i ];  
  150. }  
  151. return newsstr;  
  152. } 
  153.  
  154. //简单编码函数(与php_decode函数对应)  
  155. function php_encode(str)  
  156. {  
  157. if ( str=='' && strlen( str)>128) return false;  
  158. for( i=0; i<strlen (str); i++){  
  159. c = ord(str[i ]);  
  160. if (c>31 && c <107) c += 20 ;  
  161. if (c>106 && c <127) c -= 75 ;  
  162. word = chr(c );  
  163. s .= word;  
  164. }  
  165. return s;  
  166. } 
  167.  
  168. //简单解码函数(与php_encode函数对应)  
  169. function php_decode(str)  
  170. {  
  171. if ( str=='' && strlen(str )>128) return false;  
  172. for( i=0; i<strlen (str); i++){  
  173. c = ord(word);  
  174. if ( c>106 && c<127 ) c = c-20;  
  175. if (c>31 && c< 107) c = c+75 ;  
  176. word = chr( c);  
  177. s .= word ;  
  178. }  
  179. return s;  
  180. } 
  181.  
  182. //简单加密函数(与php_decrypt函数对应)  
  183. function php_encrypt(str)  
  184. {  
  185. encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890';  
  186. decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';  
  187. if ( strlen(str) == 0) return false;  
  188. for (i=0; i<strlen(str); i ++){  
  189. for (j=0; j <strlen(encrypt_key); j ++){  
  190. if (str[i] == encrypt_key [j]){  
  191. enstr .= decrypt_key[j];  
  192. break;  
  193. }  
  194. }  
  195. }  
  196. return enstr;  
  197. } 
  198.  
  199. //简单解密函数(与php_encrypt函数对应)  
  200. function php_decrypt(str)  
  201. {  
  202. encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890';  
  203. decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';  
  204. if ( strlen(str) == 0) return false;  
  205. for (i=0; i<strlen(str); i ++){  
  206. for (j=0; j <strlen(decrypt_key); j ++){  
  207. if (str[i] == decrypt_key [j]){  
  208. enstr .= encrypt_key[j];  
  209. break;  
  210. }  
  211. }  
  212. }  
  213. return enstr;  
  214. }  
  215. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表