Simple PHP Function Random Characters

Posted by admin
I copy a random character function on PHP.net and modify it because I have notice that if you generate a thousand random character by having it in loop the function generates less than the character limit that you have specified. So my idea is to validate the function result and if it returns less than the limit characters it will execute the function again.

function random_char( $limit=5, $char="abcdefghijklmnpqrstuvwxyz0123456789" ){

for($x = 1; $x <= $limit; $x++){
$new_string .= substr($char, mt_rand(-1, -(strlen($char))), 1 );
}

while(strlen($new_string) < $limit){
$new_string = substr(random_char( $limit ),0,4);
}

return $new_string;
}
Back to List | Comments (1) | October 03, 2009 at 03:19 pm

1 Responses

Roul Trick says:
Aug 30, 2009 at 10:53 pm

Sometimes it's really that simple, isn't it? I feel a little stupid for not thinking of this myself/earlier, though.

Leave a Reply

Name (required)

Email (required) will not be publish

Website (optional) ex: http://www.domain.com

Notify me of followup comments via e-mail. (optional)

Your Message (required)

Enter Security Code (required)
captcha  

Latest News (2)

iWebProvider.com No 1 at Yahoo search engine for the keyword -filipino web developer.

Posted on October 02, 2009

On Sept 6, 2009 iWebProvider.com is No. 1 at Yahoo and Google for keyword "filipino web developer".

continue reading

Launching of the updated iWebProvider.com

Posted on October 02, 2009

Now as of July 7, 2009 the launching of iWebProvider.com with its improve feature and the used of its very own CMS. It now has features like Blogs, comments on News articles and the upcoming user registration.

continue reading

Wise Words

"It's not about how many you have learned but how much you have utilized and mastered what you have learned."

- by: Mark Lester
Get subscribers