settingsAccountsettings
Menusettings

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>
<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>

 

answered by user eiorgert
selected by user golearnweb
...