[Solved] Sort Text Lines in PHP

  

3
Topic starter
  • Write a PHP script that sorts the text lines from a <textarea>
  • Split by new line "\n". Warning: '\n' does not work!
  • Remove \r and spaces when exist
  • Sort as string values
  • Warning: '\n' will not work!
  • Never put space  between <textarea> and </textarea>
1 Answer
2

Here is the code (NOTE: line #18):

<!DOCTYPE html>
<html>
<head>
    <title>Sort Lines in PHP</title>
</head>
<body>
 
<?php
if (isset($_GET['lines'])) {
    $lines = $_GET['lines'];
    $lines = explode("\n", $lines);
    $lines = array_map('trim', $lines);
    sort($lines, SORT_STRING);
}
?>
 
<form>
    <div><textarea rows="10" name="lines"><?php if (isset($lines)) echo implode("\n", $lines) ?></textarea></div>
    <div><input type="submit" value="SUBMIT"></div>
</form>
</body>
</html>
Share: