Guest User

Untitled

a guest
Jun 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. function fixCase($str)
  2. {
  3. $uc = 0;
  4. $lc = 0;
  5. for($i=0;$i<strlen($str);$i++)
  6. {
  7. if ($str[$i] >= 'a' && $str[$i] <= 'z')
  8. $lc++;
  9. else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
  10. $uc++;
  11. }
  12.  
  13. if ($uc == 0 || $lc == 0)
  14. {
  15. return ucwords(strtolower($str));
  16. }
  17. }
  18.  
  19. function fixCase($str)
  20. {
  21. if (
  22. (strcmp($str, strtolower($str)) === 0) ||
  23. (strcmp($str, strtoupper($str)) === 0) )
  24. {
  25. $str = ucwords(strtolower($str));
  26. }
  27.  
  28. return $str;
  29. }
  30.  
  31. // returns 0 if non-alphabetic char, 1 if uppercase, 2 if lowercase
  32. function getCharType($char)
  33. {
  34. if ($char >= 'A' && $char <= 'Z')
  35. {
  36. return 1;
  37. }
  38. else if ($char >= 'a' && $char <= 'z')
  39. {
  40. return 2;
  41. }
  42. else
  43. {
  44. return 0;
  45. }
  46. }
  47.  
  48. function fixCase($str)
  49. {
  50. for ($i = 0; $i < strlen($str); $i++)
  51. {
  52. $charType = getCharType($str[$i]);
  53. if ($charType != 0)
  54. {
  55. $firstCharType = $charType;
  56. break;
  57. }
  58. }
  59.  
  60. for ($i = $i + 1; $i < strlen($str); $i++)
  61. {
  62. $charType = getCharType($str[$i]);
  63. if ($charType != $firstCharType && $charType != 0)
  64. {
  65. return $str;
  66. }
  67. }
  68.  
  69. if ($firstCharType == 1) // uppercase, need to convert to lower first
  70. {
  71. return ucwords(strtolower($str));
  72. }
  73. else if ($firstCharType == 2) // lowercase, can just ucwords() it
  74. {
  75. return ucwords($str);
  76. }
  77. else // there were no letters at all in the string, just return it
  78. {
  79. return $str;
  80. }
  81. }
  82.  
  83. function getStringCase($subject)
  84. {
  85. if (!empty($subject))
  86. {
  87. if (preg_match('/^[^A-Za-z]+$/', $subject))
  88. return 0; // no alphabetic characters
  89. else if (preg_match('/^[^A-Z]+$/', $subject))
  90. return 1; // lowercase
  91. else if (preg_match('/^[^a-z]+$/', $subject))
  92. return 2; // uppercase
  93. else
  94. return 3; // mixed-case
  95. }
  96. else
  97. {
  98. return 0; // empty
  99. }
  100. }
  101.  
  102. function fixCase($str)
  103. {
  104. return ucwords(strtolower($str));
  105. }
  106.  
  107. function method1($str)
  108. {
  109. if (strcmp($str, strtolower($str)) == 0)
  110. {
  111. return ucwords($str);
  112. }
  113. else if (strcmp($str, strtoupper($str)) == 0)
  114. {
  115. return ucwords(strtolower($str));
  116. }
  117. else
  118. {
  119. return $str;
  120. }
  121. }
  122.  
  123. // returns 0 if non-alphabetic char, 1 if uppercase, 2 if lowercase
  124. function getCharType($char)
  125. {
  126. if ($char >= 'A' && $char <= 'Z')
  127. {
  128. return 1;
  129. }
  130. else if ($char >= 'a' && $char <= 'z')
  131. {
  132. return 2;
  133. }
  134. else
  135. {
  136. return 0;
  137. }
  138. }
  139.  
  140. function method2($str)
  141. {
  142. for ($i = 0; $i < strlen($str); $i++)
  143. {
  144. $charType = getCharType($str[$i]);
  145. if ($charType != 0)
  146. {
  147. $firstCharType = $charType;
  148. break;
  149. }
  150. }
  151.  
  152. for ($i = $i + 1; $i < strlen($str); $i++)
  153. {
  154. $charType = getCharType($str[$i]);
  155. if ($charType != $firstCharType && $charType != 0)
  156. {
  157. return $str;
  158. }
  159. }
  160.  
  161. if ($firstCharType == 1) // uppercase, need to convert to lower first
  162. {
  163. return ucwords(strtolower($str));
  164. }
  165. else if ($firstCharType == 2) // lowercase, can just ucwords() it
  166. {
  167. return ucwords($str);
  168. }
  169. else // there were no letters at all in the string, just return it
  170. {
  171. return $str;
  172. }
  173. }
  174.  
  175. function method0($str)
  176. {
  177. $uc = 0;
  178. $lc = 0;
  179. for($i=0;$i<strlen($str);$i++)
  180. {
  181. if ($str[$i] >= 'a' && $str[$i] <= 'z')
  182. $lc++;
  183. else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
  184. $uc++;
  185. }
  186.  
  187. if ($uc == 0 || $lc == 0)
  188. {
  189. return ucwords(strtolower($str));
  190. }
  191. }
  192.  
  193.  
  194. function test($func,$s)
  195. {
  196. $start = gettimeofday(true);
  197. for($i = 0; $i < 1000000; $i++)
  198. {
  199. $s4 = $func($s);
  200. }
  201. $end = gettimeofday(true);
  202. echo "$func Time: " . ($end-$start) . " - Avg: ".sprintf("%.09f",(($end-$start)/1000000))."n";
  203. }
  204.  
  205.  
  206. $s1 = "first String";
  207. $s2 = "second string";
  208. $s3 = "THIRD STRING";
  209.  
  210. test("method0",$s1);
  211. test("method0",$s2);
  212. test("method0",$s3);
  213.  
  214. test("method1",$s1);
  215. test("method1",$s2);
  216. test("method1",$s3);
  217.  
  218. test("method2",$s1);
  219. test("method2",$s2);
  220. test("method2",$s3);
  221.  
  222. method0 Time: 19.2899270058 - Avg: 0.000019290
  223. method0 Time: 20.8679389954 - Avg: 0.000020868
  224. method0 Time: 24.8917310238 - Avg: 0.00002489
  225. method1 Time: 3.07466816902 - Avg: 0.000003075
  226. method1 Time: 2.52559089661 - Avg: 0.000002526
  227. method1 Time: 4.06261897087 - Avg: 0.000004063
  228. method2 Time: 19.2718701363 - Avg: 0.000019272
  229. method2 Time: 35.2485661507 - Avg: 0.000035249
  230. method2 Time: 29.3357679844 - Avg: 0.000029336
Add Comment
Please, Sign In to add comment