By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Sort Text Lines in PHP

+2 votes
  • 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>
asked in PHP category by user andrew

1 Answer

+1 vote
Best answer

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

<!DOCTYPE html>
    <title>Sort Lines in PHP</title>

if (isset($_GET['lines'])) {
    $lines = $_GET['lines'];
    $lines = explode("\n", $lines);
    $lines = array_map('trim', $lines);
    sort($lines, SORT_STRING);

    <div><textarea rows="10" name="lines"><?php if (isset($lines)) echo implode("\n", $lines) ?></textarea></div>
    <div><input type="submit" value="SUBMIT"></div>


answered by user eiorgert
selected by user golearnweb