Increment

Increments numbers in text with $new and $last magic words (see the example).

Source code (hide)

Global files are not shown below.

Table of contents
  1. index.php

index.php

back to toc
<?php
/* globals, templates */
$locals = array (
    
'title'       => 'Increment',
    
'description' => 'Increments numbers in text with $new and $last magic words (see the <a href="index.php?example=1" title="example">example</a>).',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

/* input */
$input = array(
    
'debug'    => $_POST['debug'],
    
'example'  => $_GET['example'],
    
'repeat'   => $_POST['repeat'],
    
'text'     => $_POST['oldtext'],
    
'offset'   => $_POST['offset'],
    
'repeatUpTo'=> $_POST['offsetmax']
);

if(
$input['example']) {
    
$input = array(
        
'step'      => 'submit',
        
'text'      => '* [[/Volume $new|Volume $last]]' "\n",
        
'repeat'    => true,
        
'offset'    => 1,
        
'offsetmax' => 25
    
);
}

/* config */
$maxrepeat 1000;
$repeatlimit $input['offset'] + $maxrepeat;

/* adjust input */
if(!$input['offset']) {
    
$input['offset'] = 0;
}
if(!
$input['repeatUpTo']) {
    
$input['repeatUpTo'] = $input['offset'];
}
else if(
$input['repeatUpTo']>$repeatlimit) {
    
$input['repeatUpTo'] = $repeatlimit;
    echo 
'<p class="neutral">Limit for \'repeat until\' field exceeded, reduced to ' $input['repeatUpTo'] . ' (offset + ' $maxrepeat ').</p>';
}

/* errors */
if($_POST and (empty($input['text']) or !preg_match('/\$new/',$input['text']))) {
    echo 
'<p class="fail">Please provide text with at least one instance of "$new".</p>';
}
else if(
$_POST) {
    
// track repetitions
    
$repeatcount = -1;

    
/* generate numbers */
    
$count $input['offset'];
    do {
        
// get text and matches
        
$output['text'] = $output['text'] . $input['text'];
        
preg_match_all('/\$new|\$last/',$output['text'],$matches);
        
$matches $matches[0];

        
// perform replacements
        
for($i=0$i<count($matches); $i++) {
            if(
$matches[$i]=='$new') {
                
$count++;
            }
            
$output['text'] = preg_replace('/\$new|\$last/e','$count',$output['text'],1);
        }
        
        
// update counter
        
$repeatcount++;
    }
    while (
$input['repeat'] && $count<$input['repeatUpTo']);

    
/* output */
    // summary
    
echo '<p class="success">Incremented input up to ' $count;
    if(
$input['repeat']) {
        echo 
' (repeated input ' $repeatcount ' times to reach requested minimum of ' $input['repeatUpTo'] . ')';
    }
    echo 
'.</p>';

    
// new text    
    
echo '<h2>Processed text</h2>';
    echo 
'<textarea rows="7" cols="100" style="height:12em;" readonly="readonly">' htmlspecialchars($output['text']) . '</textarea>';
}

/* debug */
gDebug(get_defined_vars());

/* input form */
?>
<h2>Input form</h2>
<p class="instructions">Enter the text you want to add the numbers to. Type "$new" to add an incremented number, or "$last" to add the last number generated (see the <a href="index.php?example=1" title="example">example</a>).</p>
<form action="index.php" method="post">
    <label for="oldtext">Text:</label>
    <textarea
        id="oldtext"
        name="oldtext"
        rows="12"
        cols="12"><?php echo htmlspecialchars($input['text']); ?></textarea><br />
        <label for="offset">Increment from</label>
        <input
            type="text"
            id="offset"
            name="offset"
            value="<?php echo htmlspecialchars($input['offset']); ?>"
            style="width:2em;"
        /><br />
        <input
            type="checkbox"
            id="repeat"
            name="repeat"
            <?php if($input['repeat']) { echo 'checked="checked";'; } ?>
        /> <label for="repeat">Repeat input</label>
        <label for="offsetmax">until increment reaches</label>
        <input
            type="text"
            id="offsetmax"
            name="offsetmax"
            value="<?php echo htmlspecialchars($input['repeatUpTo']); ?>"
            style="width:3em;"
        /> <label for="offsetmax">(max is offset + <?php echo $maxrepeat?>)</label><br />
        <?php gDebugOption(); ?>
        <input type="submit" value="Process" /><br />
        <input type="reset" value="Reset form" />
    </fieldset>
</form>

<?php
/* globals, templates */
makeFooter();
?>

Input form

Enter the text you want to add the numbers to. Type "$new" to add an incremented number, or "$last" to add the last number generated (see the example).






This tool is written and copyright by Jesse Plamondon-Willard (Pathoschild). You may freely use, distribute, and modify this script in any way and for any purpose, so long as you cite the above name as original author.