SHOW:
|
|
- or go back to the newest paste.
1 | - | /** |
1 | + | <?php |
2 | - | * PROJECT CLEVERWEB GENERAL LESSER LICENSE INFORMATION v3.1 |
2 | + | /* |
3 | - | * ------------------------------------------------------------ |
3 | + | Cleaned up and sped up. |
4 | - | * Copyright (C) 2011-2012 Nicholas Jordon |
4 | + | 1000 interations of producing 2000 char string: |
5 | - | * All rights reserved |
5 | + | Original - 4.1486458778381 seconds |
6 | - | * Package: Project CleverWeb |
6 | + | This rewrite - 2.3620591163635 |
7 | - | * http://ProjectCleverWeb.com |
7 | + | %43 percent improvement |
8 | - | * |
8 | + | */ |
9 | - | * License: |
9 | + | |
10 | - | * GNU LESSER GENERAL PUBLIC LICENSE VERSION 3 |
10 | + | function cw_randchar( $length=NULL, $case=NULL, $spaces=false){ |
11 | - | * |
11 | + | // setting $spaces to null, then checking if null, is a waste of resources and effort. Just set it to false. Then the user can switch it on if they desire. |
12 | - | * This software is free software: you can redistribute it |
12 | + | |
13 | - | * and/or modify it under the terms of the GNU Lesser |
13 | + | |
14 | - | * General Public License as published by the Free Software |
14 | + | if(strpos($temp_length,',') !== false){ // strpos is faster/less resource intensive as strstr |
15 | - | * Foundation, version 3 of the License, You may not use this |
15 | + | $temp_length = str_replace(',','',$temp_length); //str_replace is faster than preg_replace(). RegEx have there place, but are huge wastes of resources. Know when to use them and when not to. This is not a case where RegEx are needed. |
16 | - | * software except in compliance with the license. |
16 | + | |
17 | - | * |
17 | + | $length = (is_numeric($temp_length))? $temp_length : strlen($length); |
18 | - | * You may obtain a copy of the license at: |
18 | + | |
19 | - | * http://www.gnu.org/licenses/lgpl-3.0.html |
19 | + | $length = (int)$length; |
20 | - | * |
20 | + | if($length === 0){ |
21 | - | * Unless required by applicable law or agreed to in writing, |
21 | + | |
22 | - | * software distributed under the license is distributed on |
22 | + | }else if($length > 2000){ |
23 | - | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
23 | + | |
24 | - | * KIND, either express or implied. |
24 | + | |
25 | - | * |
25 | + | // Note with strpos() - it can return a position of 0 (beginning of the string), so you need to check against the boolean false, to ensure PHP doesn't interprut the 0 as false; |
26 | - | * Verfication Key: e450edcbe7703e40d9edcf19a18fb4bc |
26 | + | if(stripos($case,'lower') !== false){ |
27 | - | */ |
27 | + | |
28 | - | function cw_randchar($length=NULL,$case=NULL,$spaces=NULL) { |
28 | + | }else if(stripos($case, 'upper') !== false){ |
29 | - | // Removes "," chars if it is a number |
29 | + | |
30 | }else{ | |
31 | $characters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
32 | - | if(strstr($length,",")){ |
32 | + | |
33 | - | $length = preg_replace("/[,]*/","",$length); |
33 | + | if($spaces){ |
34 | - | if(!is_numeric($length)){ |
34 | + | $characters .= ($case)? str_pad('',16,' ') : str_pad('',8,' '); |
35 | - | $length = strlen($temp_length); |
35 | + | |
36 | - | } |
36 | + | // No need for the is_bool() check as default is false |
37 | $chrLen = strlen($characters) - 1; // Counting the strlen each time, possibly 2000 times, is a waste of resources. (- 1) to remove you blank character errors. | |
38 | - | elseif(!is_numeric($length)){ |
38 | + | $rString = ''; // The return String. Declare before manipulating it, otherwise it will throw Warning Notice |
39 | - | $length = strlen($length); |
39 | + | for($i=0; $i<$length;++$i){ |
40 | $rString .= $characters{mt_rand(0,($chrLen))}; // {} not [] -> [] are for arrays. PHP will process it properly now, but it won't in the future. | |
41 | } | |
42 | - | if($length==NULL||$length==0){ |
42 | + | return $rString; |
43 | - | // Best password length |
43 | + | } |
44 | ||
45 |