First i would like to introduce two string function:
1 .Chr() function
The chr() function returns a character from the specified ASCII value.
syntax: chr(ascii)
How to use the chr() function:
<?php
echo chr(52).”<br />”;
echo chr(052).”<br />”;
echo chr(0x52).”<br />”;
?>
The output of the code above will be:
4
*
R
2 . rand() function
The rand() function generates a random integer.
If this function is called without parameters, it returns a random integer between 0 and RAND_MAX.
If you want a random number between 10 and 100 (inclusive), use rand (10,100).
Syntax
rand(min,max)
Example
<?php
echo(rand(10,100))
?>
Output:
67(or the number between 10 and 100)
ok lets move to the real tutorial. This tutorial will teach you how to generate the random string.
Coding:
<?
$lengthofstring = 10;
while($i < $lengthofstring ) {
$x=1;
$y=3;
$part = rand($x,$y); //returns either 1 or 2 or 3
if($part==1){$a=48;$b=57;} // Numbers
if($part==2){$a=65;$b=90;} // UpperCase
if($part==3){$a=97;$b=122;} // LowerCase
$code_part=chr(rand($a,$b)); // returns the character for the randomly generated character
$i++;
$newstring = $newstring.$code_part;
}
echo $newstring;
?>
Try this out…..
PHP Arrays
PHP While Loops
PHP For Loops
PHP Functions
PHP Forms
PHP $_GET
PHP $_POST
PHP Advanced
PHP Date
PHP Include
PHP File
PHP File Upload
PHP Cookies
PHP Sessions
PHP E-mail
PHP Secure E-mail
PHP Error
PHP Exception
PHP Filter
PHP Database
MySQL Introduction
MySQL Connect
MySQL Create
MySQL Insert
MySQL Select
MySQL Where
MySQL Order By
MySQL Update
MySQL Delete
PHP ODBC
PHP XML
XML Expat Parser
XML DOM
XML SimpleXML
PHP and AJAX
AJAX Intro
AJAX PHP
AJAX Database
AJAX XML
AJAX Live Search
AJAX RSS Reader
AJAX Poll
PHP Reference
PHP Array
PHP Calendar
PHP Date
PHP Directory
PHP Error
PHP Filesystem
PHP Filter
PHP FTP
PHP HTTP
PHP Libxml
PHP Mail
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Quiz
PHP rand() Function
Complete PHP Math Reference
Definition and Usage
The rand() function generates a random integer.
If this function is called without parameters, it returns a random integer between 0 and RAND_MAX.
If you want a random number between 10 and 100 (inclusive), use rand (10,100).
Syntax
rand(min,max)