Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fixCase($str)
- {
- $uc = 0;
- $lc = 0;
- for($i=0;$i<strlen($str);$i++)
- {
- if ($str[$i] >= 'a' && $str[$i] <= 'z')
- $lc++;
- else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
- $uc++;
- }
- if ($uc == 0 || $lc == 0)
- {
- return ucwords(strtolower($str));
- }
- }
- function fixCase($str)
- {
- if (
- (strcmp($str, strtolower($str)) === 0) ||
- (strcmp($str, strtoupper($str)) === 0) )
- {
- $str = ucwords(strtolower($str));
- }
- return $str;
- }
- // returns 0 if non-alphabetic char, 1 if uppercase, 2 if lowercase
- function getCharType($char)
- {
- if ($char >= 'A' && $char <= 'Z')
- {
- return 1;
- }
- else if ($char >= 'a' && $char <= 'z')
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- function fixCase($str)
- {
- for ($i = 0; $i < strlen($str); $i++)
- {
- $charType = getCharType($str[$i]);
- if ($charType != 0)
- {
- $firstCharType = $charType;
- break;
- }
- }
- for ($i = $i + 1; $i < strlen($str); $i++)
- {
- $charType = getCharType($str[$i]);
- if ($charType != $firstCharType && $charType != 0)
- {
- return $str;
- }
- }
- if ($firstCharType == 1) // uppercase, need to convert to lower first
- {
- return ucwords(strtolower($str));
- }
- else if ($firstCharType == 2) // lowercase, can just ucwords() it
- {
- return ucwords($str);
- }
- else // there were no letters at all in the string, just return it
- {
- return $str;
- }
- }
- function getStringCase($subject)
- {
- if (!empty($subject))
- {
- if (preg_match('/^[^A-Za-z]+$/', $subject))
- return 0; // no alphabetic characters
- else if (preg_match('/^[^A-Z]+$/', $subject))
- return 1; // lowercase
- else if (preg_match('/^[^a-z]+$/', $subject))
- return 2; // uppercase
- else
- return 3; // mixed-case
- }
- else
- {
- return 0; // empty
- }
- }
- function fixCase($str)
- {
- return ucwords(strtolower($str));
- }
- function method1($str)
- {
- if (strcmp($str, strtolower($str)) == 0)
- {
- return ucwords($str);
- }
- else if (strcmp($str, strtoupper($str)) == 0)
- {
- return ucwords(strtolower($str));
- }
- else
- {
- return $str;
- }
- }
- // returns 0 if non-alphabetic char, 1 if uppercase, 2 if lowercase
- function getCharType($char)
- {
- if ($char >= 'A' && $char <= 'Z')
- {
- return 1;
- }
- else if ($char >= 'a' && $char <= 'z')
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- function method2($str)
- {
- for ($i = 0; $i < strlen($str); $i++)
- {
- $charType = getCharType($str[$i]);
- if ($charType != 0)
- {
- $firstCharType = $charType;
- break;
- }
- }
- for ($i = $i + 1; $i < strlen($str); $i++)
- {
- $charType = getCharType($str[$i]);
- if ($charType != $firstCharType && $charType != 0)
- {
- return $str;
- }
- }
- if ($firstCharType == 1) // uppercase, need to convert to lower first
- {
- return ucwords(strtolower($str));
- }
- else if ($firstCharType == 2) // lowercase, can just ucwords() it
- {
- return ucwords($str);
- }
- else // there were no letters at all in the string, just return it
- {
- return $str;
- }
- }
- function method0($str)
- {
- $uc = 0;
- $lc = 0;
- for($i=0;$i<strlen($str);$i++)
- {
- if ($str[$i] >= 'a' && $str[$i] <= 'z')
- $lc++;
- else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
- $uc++;
- }
- if ($uc == 0 || $lc == 0)
- {
- return ucwords(strtolower($str));
- }
- }
- function test($func,$s)
- {
- $start = gettimeofday(true);
- for($i = 0; $i < 1000000; $i++)
- {
- $s4 = $func($s);
- }
- $end = gettimeofday(true);
- echo "$func Time: " . ($end-$start) . " - Avg: ".sprintf("%.09f",(($end-$start)/1000000))."n";
- }
- $s1 = "first String";
- $s2 = "second string";
- $s3 = "THIRD STRING";
- test("method0",$s1);
- test("method0",$s2);
- test("method0",$s3);
- test("method1",$s1);
- test("method1",$s2);
- test("method1",$s3);
- test("method2",$s1);
- test("method2",$s2);
- test("method2",$s3);
- method0 Time: 19.2899270058 - Avg: 0.000019290
- method0 Time: 20.8679389954 - Avg: 0.000020868
- method0 Time: 24.8917310238 - Avg: 0.00002489
- method1 Time: 3.07466816902 - Avg: 0.000003075
- method1 Time: 2.52559089661 - Avg: 0.000002526
- method1 Time: 4.06261897087 - Avg: 0.000004063
- method2 Time: 19.2718701363 - Avg: 0.000019272
- method2 Time: 35.2485661507 - Avg: 0.000035249
- method2 Time: 29.3357679844 - Avg: 0.000029336
Add Comment
Please, Sign In to add comment