NameSilo

Alphabetizing a textbox

SpaceshipSpaceship
Watch
Impact
98
I tried searching google, but didn't find anything.

I want to post data into a textbox and click a button to alphabetize the data either A to Z or Z to A

Does anyone know of a code to do such a thing?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
If you can get all the pieces of data into an array, then you can do it using PHP's sort() function, example:
PHP:
$names = array('lee101','jdk');
sort($names);
print_r($names);
Will output all values in the $names array alphabetically
 
0
•••
PHP:
<?php
/*
	Alphabetizes the content of a textarea on a line-by-line basis.
	Sorting is case-insensitive.
	Blank lines are removed.
*/

if (isset($_POST['content'])) // alphabetize everything
{
	// break the text area into lines
	$lines = explode("\n", $_POST['content']);
	
	// now remove blank lines
	function strip_blanks($val) {return trim($val) == '' ? false : true;}
	$lines = array_filter($lines,strip_blanks);
	
	// now alphabetize the array.  The array is passed as a reference,
	// and sorting is done in-situ
	natcasesort($lines);
		
	// make the list display safe
	$sorted = htmlentities(implode("\n",$lines));
}
else // initialize
{
	$sorted = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Textarea Sorter</title>
	
</head>
<body>
	<p>
		Enter or paste a list of items.  When you press the "sort" button, 
		the list will be sorted alphabetically.  Case is ignored.
	</p>
	<form action="" method="post">
	    <textarea cols="64" rows="10" name="content"><?php echo $sorted; ?></textarea><br />
	    <input type="submit" value="sort" />
	</form>
</body>
</html>
 
0
•••
Thanks both of you. I think I have what I need to make what I am trying to. I will create something else to sort in reverse order.
 
0
•••
Oops, forgot about the reverse. That only requires one extra optional call, and another button on the form.

I'll update shortly.

With reverse sorting added:

PHP:
<?php
/*
	Alphabetizes the content of a textarea on a line-by-line basis.
	Sorting is case-insensitive.
	Blank lines are removed.
	One form button for forward sorting, another for reverse sorting.
*/

if (isset($_POST['content'])) // alphabetize everything
{
	// break the text area into lines
	$lines = explode("\n", $_POST['content']);
	
	// now remove blank lines
	function strip_blanks($val) {return trim($val) == '' ? false : true;}
	$lines = array_filter($lines,strip_blanks);
	
	// now alphabetize the array.  The array is passed as a reference,
	// and sorting is done in-situ
	natcasesort($lines);
		
	// now see if we're reverse sorting
	if ($_POST['rsort'])
		$lines = array_reverse($lines,false);
		
	// make the list display safe
	$sorted = htmlentities(implode("\n",$lines));
}
else // initialize
{
	$sorted = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Textarea Sorter</title>
	
</head>
<body>
	<p>
		Enter or paste a list of items.  When you press the "sort" button, 
		the list will be sorted alphabetically.  Case is ignored.
	</p>
	<form action="" method="post">
	    <textarea cols="64" rows="10" name="content"><?php echo $sorted; ?></textarea><br />
	    <input type="submit" name="sort" value="sort" />
	    <input type="submit" name="rsort" value="reverse sort" />
	</form>
</body>
</html>
 
0
•••
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomainEasy โ€” Payment Flexibility
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back