Plot Ideas generator.">
Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryGenerators and Converters

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Idea Generator, Part I

This is the first of a planned three-part tutorial for making something similar to the Plot Ideas generator.

Plot Ideas is for fiction writers. A similar generator can be for sales copy writers. Or for other marketing ideas, perhaps for product descriptions.

To be clear, this is not a copy generator. Instead, it is an idea generator.

Ideas may be stimulated when seeing familiar items in odd or unexpected combinations. That method of idea stimulation is what this software is about.

The writing of the second part will be started as soon as this one is finished. Still, other articles may be published before the second part is available. After that, the third part.

When another part of the tutorial is ready, it will be the Possibilities newsletter article for that week.

This first part contains a content template and initial replacement lists. An idea is generated and published when the page loads.

The planned second part is intended to describe how to generate additional ideas with a tap on a button or link. The third, for allowing the user to provide their own lists of items for the idea generator to use.

In the bordered paragraph below is a live-generated idea using the software presented in this article.

The fox leaped over the campfire!

For another generated idea, reload this page.

The generated idea will not always be different than the previously generated idea. The software does not remember what the previous idea was — it just goes ahead and generates one.

Here is the source code for the idea generation software. Following the source code, we'll talk about customization.

<?php
/* String-Replacement Content Generator
   Version 1.0
   April 30, 2021
   Will Bontrager Software LLC
   https://www.willmaster.com/
*/

/*
There are three customizations. 
The first is a template for the content.
The last two are lists of replacements for 
  use in the template.
See accompanying article for instructions.
*/

// Template for content.
$ContentTemplate = <<<END
The {REPLACEMENT1} over the {REPLACEMENT2}!
END;

// List of replacements 1 content, one list item per line.
$Replacement1List = <<<END
cow jumped
fox leaped
crow flew
ant crawled
END;

// List of replacements 2 content, one list item per line.
$Replacement2List = <<<END
moon
campfire
radish plant
END;

/* End of customizations */

// Conform replacement lists.
$indice = 0;
$Lists = array();
for( $i=0; $i<=100; $i++ ) // Update number if largest number in list variables ($Replacement###List) is more than 100.
{
   $s = "\$Replacement{$i}List";
   $b = false;
   $s = "\$b=empty($s);";
   eval($s);
   if($b) { continue; }
   $s = "\$Lists[$indice]=\"$i\t\$Replacement{$i}List\";";
   $indice++;
   eval($s);
}
$ListsLength = count($Lists);
for( $i=0; $i<$ListsLength; $i++ )
{
   $ta = explode("\t",trim($Lists[$i]),2);
   $Lists[$i] = preg_split('/[\r\n]+/',$ta[1]);
   $Lists[$i]['key'] = $ta[0];
}

// Select list items and replace placeholders in content.
$content = $ContentTemplate;
for( $i=0; $i<$ListsLength; $i++ )
{
   $key = $Lists[$i]['key'];
   unset($Lists[$i]['key']);
   $replacement = SelectAnItem($Lists[$i]);
   $content = str_replace("{REPLACEMENT$key}",$replacement,$content);
}

// Reply with the generated content.
echo $content;

function SelectAnItem($arr)
{
   $count = count($arr)-1;
   $itemIndice = mt_rand(0,$count);
   return $arr[$itemIndice];
}
?>

The software will work without customization.

But, customization is how you make it generate ideas in alignment with your purpose.

The software has three places to customize, the idea template and two lists of words/phrases to insert into the template.

The Idea Template —

Let's do the idea template first.

In the software source code, you'll see the idea template between the first lines that contain <<<END and END;. Here is the template content.

The {REPLACEMENT1} over the {REPLACEMENT2}!

You'll notice two replacement placeholders, {REPLACEMENT1} and {REPLACEMENT2}. Notice the number before the closing curly brace. That is how the software knows which word/phrase list to use when selecting a list item to replace it with.

The template content may be multi-line and have more or less replacement placeholders.

The First Words/Phrases List —

Now, here is the first of the two words/phrases lists.

$Replacement1List = <<<END
cow jumped
fox leaped
crow flew
ant crawled
END;

Notice the digit 1 within the $Replacement1List variable name. ($Replacement1List)

Each word/phrase list has a unique number. The number matches the placeholder in the template content. Thus, {REPLACEMENT1} in the idea template is replaced with a word/phrase randomly selected from $Replacement1List.

The words/phrases for $Replacement1List may be changed, removed, replaced, added to — in other words, it may contain whatever words or phrases you want it to contain.

The list of words/phrases are one per line. Blank lines are ignored.

The Second Words/Phrases List —

Here is the second of the two words/phrases lists.

$Replacement2List = <<<END
moon
campfire
radish plant
END;

Notice the digit 2 within the $Replacement2List variable name.

Placeholder {REPLACEMENT2} in the idea template is replaced with a word/phrase randomly selected from the $Replacement2List list.

The words/phrases in the $Replacement2List may be updated for whatever words or phrases you want it to contain.

Additional Customization Notes —

The idea template in the above source code has placeholders for two replacements, {REPLACEMENT1} and {REPLACEMENT2}. There may be more or less placeholders, a few or a bunch.

The digit for each placeholder needs to be less than 99, and generally should be unique. (If the same number is used within two or more placeholders, an identical replacement is performed for each.)

There needs to be a $Replacement##List variable (replace "##" with a number) containing a list of one or more words/phrases to select from for each placeholder in the idea template. If any are missing, the placeholder in the idea template will not be replaced.

Each $Replacement##List variable ("##" replaced with a number) may contain as few or many words/phrases as you wish. But each must be on a line by itself. (Blank lines are ignored.)

If a $Replacement##List variable is specified without a corresponding idea template placeholder, the $Replacement##List variable is ignored.

What all that means is that there generally should be one $Replacement##List variable for each {REPLACEMENT##} placeholder in the idea template. Any that don't have a corresponding match are ignored.

One more thing, the ## for the {REPLACEMENT##} placeholders do not have to be sequential. The software is very lenient about that.

Installing the Software —

To install the software, upload it to a directory on your domain accessible with a browser. Name the file IdeaGenerator.php (or other *.php file name that you prefer).

To test the software, type its URL into your browser. It should respond with a randomly generated idea.

To insert randomly generated ideas into your web page, include IdeaGenerator.php with the include() command. Here is an example (assuming the script is installed at /ideas/IdeaGenerator.php:

<?php include("{$_SERVER['DOCUMENT_ROOT']}/ideas/IdeaGenerator.php"); ?>

The idea generator will now insert a randomly generated idea into the web page at that location.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC