PHP Classes

PHP Random Text Generator: Generate test data from word files

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 224 All time: 8,208 This week: 150Up
Version License PHP version Categories
textrandomgenerator 1.1GNU General Publi...5PHP 5, Text processing, Testing
Description 

Author

This class can generate test data from word files.

It reads files with words of several types like female and male first names, surnames, verbs, pronouns, questions, etc..

The class can generate random text of specific types from random words read from files, like sentences with variable number of words, male and female first and last names, or common values for testing applications like random login names, email addresses, random age, etc..

Currently the package provides files with words for English and Russian.

Innovation Award
PHP Programming Innovation award nominee
April 2017
Number 7
Some applications need to be tested using fake data that must be as close as possible to real data.

This class can generate random text for testing purposes that can simple values like random names, or more complex text like complete sentences in different languages.

Manuel Lemos
Picture of Max Stemplevski
  Performance   Level  
Name: Max Stemplevski <contact>
Classes: 12 packages by
Country: Belarus Belarus
Age: 35
All time rank: 19438 in Belarus Belarus
Week rank: 312 Up2 in Belarus Belarus Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php
/***
 * Example of using of TextGenerator class file
 */
use RandomText\TextGenerator;
if (
file_exists('TextGenerator.php')) {
    include_once(
'TextGenerator.php');
} else {
    die(
'TextGenerator class not found!');
}

TextGenerator::initialize(isset($_REQUEST['lang']) ? $_REQUEST['lang'] : 'eng');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Text Random Generator</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
          integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"
          integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
            integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
            crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
    <div class="row row-offcanvas row-offcanvas-right">
        <div class="col-xs-12 col-sm-12">
            <div class="jumbotron">
                <h1>Text Template Generator!</h1>

                <p>This is an example to show the potential of text generation.</p>

                <p>
                    <?= TextGenerator::generateRandomSentences(1, 5); ?>
</p>
            </div>
            <div class="row">
                <?php
               
for ($i = 1; $i <= 3; $i++) {
                   
?>
<div class="col-xs-6 col-lg-4">
                        <h2><?= TextGenerator::generateRandomHeader(); ?></h2>
                        <?= TextGenerator::generateRandomSentences(5, 5); ?>
<p><a role="button" href="#"
                              class="btn btn-default"><?= TextGenerator::generateRandomSentences(1, 1); ?> »</a></p>
                    </div>
                <?php
               
}
               
?>
</div>
            <div class="jumbotron">
                <h1>Message example</h1>

                <p style="text-align: justify;"><?= TextGenerator::generateRandomSentences(rand(5, 12), rand(3, 7)); ?></p>

                <p><a class="btn btn-primary btn-lg" href="#"
                      role="button"><?= TextGenerator::generateRandomSentences(1, 1); ?></a></p>
            </div>
            <div class="row">
                <?php
               
for ($i = 1; $i <= 3; $i++) {
                   
?>
<div class="col-xs-6 col-lg-4">
                        <h2><?= TextGenerator::generateRandomHeader(); ?></h2>
                        <?= TextGenerator::generateRandomSentences(5, 5); ?>
<p><a role="button" href="#"
                              class="btn btn-default"><?= TextGenerator::generateRandomSentences(1, 1); ?> »</a></p>
                    </div>
                <?php
               
}
               
?>
</div>
            <div class="jumbotron">
                <h1>API example:</h1>

                <p style="text-align: justify;">API secret: <?= TextGenerator::generateRandomString(30); ?></p>

                <p style="text-align: justify;">Password: <?= TextGenerator::generateRandomString(12, true); ?></p>
            </div>
            <div class="jumbotron">
                <h1>User data generate:</h1>
                <table class="table table-hover">
                    <tr>
                        <th>Picture</th>
                        <th>First Name</th>
                        <th>Second Name</th>
                        <th>Gender</th>
                        <th>Age</th>
                        <th>Birthday</th>
                        <th>Email</th>
                        <th>Login</th>
                        <th>Password</th>
                    </tr>
                    <?php
                   
for ($i = 0; $i < 25; $i++) {
                       
$gender = rand(0, 1) ? 'Male' : 'Female';
                       
$firstname = TextGenerator::generateFirstName($gender);
                       
$lastname = TextGenerator::generateLastName($gender);
                       
$age = TextGenerator::generateYearsOld(18, 68);
                       
$birthday = TextGenerator::generateBirthday($age);
                       
$email = TextGenerator::generateEmail($firstname, $lastname, $birthday);
                       
$login = TextGenerator::generateLogin($firstname, $lastname, $birthday);
                       
$password = TextGenerator::generateRandomString(12, true);
                       
?>
<tr>
                            <td><img height="25"
                                     src="https://graph.facebook.com/<?= rand(500000, 1000000); ?>/picture?type=square"/>
                            </td>
                            <td><?= $firstname ?></td>
                            <td><?= $lastname ?></td>
                            <td><?= $gender; ?></td>
                            <td><?= $age; ?></td>
                            <td><?= $birthday; ?></td>
                            <td><?= $email; ?></td>
                            <td><?= $login; ?></td>
                            <td><?= $password; ?></td>
                        </tr>
                    <?php
                   
}
                   
?>
</table>
            </div>
            <div class="jumbotron">
                <h1>Car info example:</h1>
                <p style="text-align: justify;">Vin code: <?= TextGenerator::generateVinCode(); ?></p>
            </div>
        </div>
    </div>
    <hr>
    <footer>
        <p>&copy; <?= date('Y'); ?> Company "<?= TextGenerator::generateRandomSentences(1, 1); ?>", Inc.</p>
    </footer>
</div>
</body>
</html>


Details

TextRandomGenerator

Generate random sentences/headers/passwords/first(last) names/emails

*

General methods:

initialize($lang = 'eng') - Use always for load words from language files

generateRandomHeader() - Generate random string for header

generateRandomString($length = 10, $spec = false) - Method for generate random string (with special symbols)

generateRandomSentences($count_of_sentences = 5, $count_of_words = 0) - Generate random sentences string

generateFirstName($gender = 'Male') - Generate random First Name from file (by gender)

generateLastName($gender = 'Male') - Generate random First Name from file (by gender)

generateYearsOld($min = 18, $max = 91) - Generate random year value (18..91)

generateBirthday($age) - Generate random birthday string by age

generateEmail($firstname, $lastname, $birthday) - Generate email (by firstname,lastname,birthday )

generateLogin($firstname, $lastname, $birthday) - Generate login (by firstname,lastname,birthday )

Example of using:

*

use RandomText\TextGenerator;
//...

TextGenerator::initialize();

$api_secret = TextGenerator::generateRandomString(30);
$api_password = TextGenerator::generateRandomString(12, true);
$gender = rand(0, 1) ? 'Male' : 'Female';
$firstname = TextGenerator::generateFirstName($gender);
$lastname = TextGenerator::generateLastName($gender);
$age = TextGenerator::generateYearsOld(18, 68);
$birthday = TextGenerator::generateBirthday($age);
$email = TextGenerator::generateEmail($firstname, $lastname, $birthday);
$login = TextGenerator::generateLogin($firstname, $lastname, $birthday);
$password = TextGenerator::generateRandomString(12, true);

> Allowed 2 languages RUS and ENG(by default)


Screenshots  
  • example1
  • Like_Lorem
  Files folder image Files  
File Role Description
Files folder imageeng (8 files)
Files folder imagerus (8 files)
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation
Plain text file TextGenerator.php Class Class source

  Files folder image Files  /  eng  
File Role Description
  Accessible without login Plain text file female_firstnames.txt Doc. Documentation
  Accessible without login Plain text file male_firstnames.txt Doc. Documentation
  Accessible without login Plain text file popular_verbs.txt Doc. Documentation
  Accessible without login Plain text file pronouns.txt Doc. Documentation
  Accessible without login Plain text file question_words.txt Doc. Documentation
  Accessible without login Plain text file surnames.txt Doc. Documentation
  Accessible without login Plain text file vincodes.txt Doc. Documentation
  Accessible without login Plain text file words.txt Doc. Documentation

  Files folder image Files  /  rus  
File Role Description
  Accessible without login Plain text file female_firstnames.txt Doc. Documentation
  Accessible without login Plain text file male_firstnames.txt Doc. Documentation
  Accessible without login Plain text file popular_verbs.txt Doc. Documentation
  Accessible without login Plain text file pronouns.txt Doc. Documentation
  Accessible without login Plain text file question_words.txt Doc. Documentation
  Accessible without login Plain text file surnames.txt Doc. Documentation
  Accessible without login Plain text file vincodes.txt Doc. Documentation
  Accessible without login Plain text file words.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 90%
Total:224
This week:0
All time:8,208
This week:150Up