Sort

Sorts a newline-delineated list.

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'       => 'Sort',
    
'description' => 'Sorts a newline-delineated list.',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

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

/* sort */
$output['array'] = explode("\n",$input['text']);

if(
$input['caseSensitive']) {
    
sort($output['array']);
}
else {
    
natcasesort($output['array']);
}

/* rebuild string */
// collapse array
$output['text'] = '';
foreach(
$output['array'] as $item) {
    
$output['text'] .= $item "\n";
}
// remove blank lines
$output['text'] = preg_replace('/^[\r\n]+/','',$output['text']);

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

// result    
if($output['text']) {
    
?>
    <h2>Sorted list</h2>
    <textarea
        rows="7"
        cols="100"
        readonly="readonly"><?php echo $output['text']; ?></textarea>
    <?php
}

// input form
?>
<h2>Input</h2>
<form action="index.php" method="post">
    <p class="instruction">Paste the newline-delineated text into the box below.</p>
    <label for="text">List</label>
    <textarea
        id="text"
        name="text"
        rows="12"
        cols="100"><?php echo $input['text']; ?></textarea>
    <input type="submit" value="Sort list" /><br />
    <input type="reset" value="Reset form" />

    <fieldset>
        <legend>Extended options</legend>
        <input
            type="checkbox"
            id="caseSensitive"
            name="caseSensitive"
            <?php gCheckBox($input['caseSensitive']); ?>
        />
        <label for="caseSensitive">case-sensitive</label><br />
        <?php gDebugOption(); ?>
    </fieldset>
</form>

<?php


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

Input

Paste the newline-delineated text into the box below.


Extended options

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.