Line numbers

Adds line numbers to poem text (see 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'       => 'Line numbers',
    
'description' => 'Adds line numbers to poem text (<a href="index.php?example=1" title="prefill with example">see example</a>).',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

/* variables */
$input = array(
    
'text'     => stripslashes($_POST['text']),
    
'example'  => $_GET['example']
);

if(
$input['example']) {
    
$input['text'] = "What passing-bells for these who die as cattle?\nOnly the monstrous anger of the guns.\nOnly the stuttering rifles' rapid rattle\nCan patter out their hasty orisons.\nNo mockeries for them; no prayers nor bells,\nNor any voice of mourning save the choirs,—\nThe shrill, demented choirs of wailing shells;\nAnd bugles calling for them from sad shires.\n\nWhat candles may be held to speed them all?\nNot in the hands of boys, but in their eyes\nShall shine the holy glimmers of goodbyes.\nThe pallor of girls' brows shall be their pall;\nTheir flowers the tenderness of patient minds,\nAnd each slow dusk a drawing-down of blinds.";
}

if(
$input['text']) {
    
/* add line numbers */
    // add placeholders
    
$output['text'] = preg_replace('/^([ \t]*[^\n\s\r])/m','$line$1',$input['text']);

    
// count lines
    
preg_match_all('/\$line/',$output['text'],$matches);
    
$matches $matches[0];
    
$count   count($matches);

    
// determine zero-padding width
    
$padding floor(log($count,10));
    if(
$padding=='-INF') {
        
$padding 0;
    }
    
$padding++;

    
// add numbers
    
$number 0;
    for(
$i=0$i<$count$i++) {
        
$number++;
        
$output['text'] = preg_replace('/^\$line/em',"str_pad($number,$padding,'0',STR_PAD_LEFT) . '  '",$output['text'],1);
    }
}

/* page content */
gDebug(get_defined_vars());
// output form
if($output['text']) {
    
?>
    <h2>Generated text</h2>
    <textarea
        rows="7"
        cols="100"
        readonly="readonly"><?php echo htmlspecialchars($output['text']); ?></textarea>
    <?php
}

// input form
?>
    <h2>Input form</h2>
    <form action="index.php" method="post">
        <label for="text">Poem text</label><br />
        <textarea
            id="text"
            name="text"
            rows="12"
            cols="100"><?php echo htmlspecialchars($input['text']); ?></textarea>
        <?php gDebugOption(); ?>
        <input type="submit" value="parse" /><br />
        <input type="reset" value="reset form" />
    </form>

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

Input form




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.