PHP Classes

PHP Random String Generator: Generates random string based on given rules

Recommend this page to a friend!
  Info   View files Example   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 236 All time: 8,077 This week: 673Up
Version License PHP version Categories
random-string 1.0GNU General Publi...5.4PHP 5, Text processing
Description 

Author

This class can generates random string based on given rules.

It can generate a random string of a given length using characters of given sets.

The generated string may have given prefix or suffix strings.

The class can generate multiple strings and assure that they are all different from each other.

Picture of Andrei Roslovtsev
Name: Andrei Roslovtsev <contact>
Classes: 1 package by
Country: Philippines Philippines
Age: 48
All time rank: 402935 in Philippines Philippines
Week rank: 360 Up4 in Philippines Philippines Up

Example

<?php
/*
 * Example usage
 * HTML form that sets parameters
 * outputs result to textarea
 * Some debugging info provided as well
 */


if (isset($_POST['input-charsets'])) {

    include (
'RandString.php');
   
$oRS = new b_and_p\RandStrNS\RandString();


   
$length = filter_input(INPUT_POST, 'input-length', FILTER_SANITIZE_NUMBER_INT);
   
$parts = filter_input(INPUT_POST, 'input-parts', FILTER_SANITIZE_NUMBER_INT);
   
$delim = filter_input(INPUT_POST, 'input-delim', FILTER_SANITIZE_STRING);
   
$prefix = filter_input(INPUT_POST, 'input-prefix', FILTER_SANITIZE_STRING);
   
$suffix = filter_input(INPUT_POST, 'input-suffix', FILTER_SANITIZE_STRING);
   
$count = filter_input(INPUT_POST, 'input-count', FILTER_SANITIZE_NUMBER_INT);
   
$unique = isset($_POST['input-unique']) ? true : false;

   
$oRS->part_length = $length; // INT
   
$oRS->parts = $parts; // INT
   
$oRS->delim = $delim; // STR
   
$oRS->prefix = $prefix; // STR
   
$oRS->suffix = $suffix; // STR
   
$oRS->count = $count; // STR
   
$oRS->unique = $unique; //BOOL

   
$flags = 0;
    if (isset(
$_POST['input-charsets'])) {
        foreach (
$_POST['input-charsets'] as $item) {
           
$flags += (int) $item;
        }
    }

   
$oRS->flags = $flags;

   
$result = $oRS->generate();
}
?>
<!doctype html>
<html>
    <head>
        <title>Random String generator</title>

        <style type="text/css">
            body {
                font-family: calibri, sans-serif
            }
            .form label {
                display:inline-block;
                width: 140px;
                font-weight: bold;
            }

            .form-group {
                margin:4px;
                padding:4px;
                border: solid 1px #ccc;
                border-radius: 6px;
                width: 400px;
                height: 1.6em;
            }
            .form-group input {
                float:right;
                padding:4px;
                margin-bottom: 4px;
            }

            #output {
                width:400px;
            }

            #output textarea {
                width:100%;
                height: 200px;
            }

        </style>

    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <div class="form">
                <div class="form-group">
                    <label for="input-length">Part length</label>
<?php
$val
= isset($length) ? $length : 2;
?>
<input type="number" min="1" name="input-length" value="<?php echo $val; ?>">
                </div>
                <div class="form-group">
                    <label for="input-parts">Parts</label>
<?php
$val
= isset($parts) ? $parts : 2;
?>
<input type="number" min="1" name="input-parts" value="<?php echo $val; ?>">
                </div>
                <div class="form-group">
                    <label for="input-delim">Delimiter</label>
<?php
$val
= isset($delim) ? $delim : '-';
?>
<input type="text" name="input-delim" value="<?php echo $val; ?>">
                </div>

                <div class="form-group">
                    <label for="input-prefix">Prefix</label>
<?php
$val
= isset($prefix) ? $prefix : '';
?>
<input type="text" name="input-prefix" value="<?php echo $val; ?>">
                </div>


                <div class="form-group">
                    <label for="input-suffix">Suffix</label>
<?php
$val
= isset($suffix) ? $suffix : '';
?>
<input type="text" name="input-suffix" value="<?php echo $val; ?>">
                </div>

                <div class="form-group">

<?php
$bitmask
= isset($flags) ? $flags : 0;
?>

                    <label for="input-charset1">Alpha lower case</label>
                    <?php
                    $checked
= ($bitmask & (1 << 0)) ? 'checked' : '';
                   
?>
<input type="checkbox" id ="input-charset1" name="input-charsets[]" value="1" <?php echo $checked; ?> >
                </div>
                <div class="form-group">
                    <label for="input-charset2">Alpha upper case</label>
<?php
$checked
= ($bitmask & (1 << 1)) ? 'checked' : '';
?>
<input type="checkbox" id ="input-charset2" name="input-charsets[]" value="2" <?php echo $checked; ?> >
                </div>
                <div class="form-group">
                    <label for="input-charset3">Numbers</label>
<?php
$checked
= ($bitmask & (1 << 2)) ? 'checked' : '';
?>
<input type="checkbox" id ="input-charset3" name="input-charsets[]" value="4" <?php echo $checked; ?> >
                </div>
                <div class="form-group">
                    <label for="input-charset4">Special characters</label>
<?php
$checked
= ($bitmask & (1 << 3)) ? 'checked' : '';
?>
<input type="checkbox" id ="input-charset4" name="input-charsets[]" value="8" <?php echo $checked; ?> >
                </div>

                <div class="form-group">
                    <label for="input-length">Count</label>
<?php
$val
= isset($count) ? $count : 2;
?>
<input type="number" min="1" name="input-count" value="<?php echo $val; ?>">
                </div>

                <div class="form-group">
                    <label for="input-length">Unique</label>
<?php
$checked
= isset($unique) && $unique;
$checked_str = $checked ? 'checked' : '';
?>
<input type="checkbox" name="input-unique" <?php echo $checked_str; ?>>
                </div>
            </div>
            <button type="submit">Submit</button>
        </form>

        <div id="output">
            <textarea readonly="true">
<?php
if (isset($oRS)) {
    if (!
$oRS->error) {
        foreach (
$result as $string) {
            echo
$string . "\n";
        }
    } else {
        echo
$oRS->error;
    }
}
?>
</textarea>
                <?php
               
if (isset($oRS)) {
                    echo
'DEBUG INFO:<br>';
                    echo
'Duplicates found in generation cycle<br>';
                   
var_dump($oRS->duplicates);
                    echo
'History - all items in generation cycle<br>';
                   
var_dump($oRS->history);
                }
               
?>
</div>
    </body>
</html>


Details

rand_string

Random String generation class


  Files folder image Files  
File Role Description
Files folder imageapigen_docs (7 files, 1 directory)
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License
Plain text file RandString.php Class Class source
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files  /  apigen_docs  
File Role Description
Files folder imageresources (11 files)
  Accessible without login HTML file 404.html Doc. Documentation
  Accessible without login HTML file annotation-group-annotation.html Doc. Documentation
  Accessible without login HTML file class-b_and_p.RandStrNS.RandString.html Doc. Documentation
  Accessible without login Plain text file elementlist.js Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login HTML file namespace-b_and_p.html Doc. Documentation
  Accessible without login HTML file namespace-b_and_p.RandStrNS.html Doc. Documentation

  Files folder image Files  /  apigen_docs  /  resources  
File Role Description
  Accessible without login Image file collapsed.png Icon Icon image
  Accessible without login Plain text file combined.js Data Auxiliary data
  Accessible without login Image file footer.png Data Auxiliary data
  Accessible without login Image file inherit.png Icon Icon image
  Accessible without login Image file resize.png Icon Icon image
  Accessible without login Image file sort.png Icon Icon image
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Image file tree-cleaner.png Icon Icon image
  Accessible without login Image file tree-hasnext.png Icon Icon image
  Accessible without login Image file tree-last.png Data Auxiliary data
  Accessible without login Image file tree-vertical.png Icon Icon image

 Version Control Unique User Downloads Download Rankings  
 100%
Total:236
This week:0
All time:8,077
This week:673Up