1

Greets Is it possible to add suffix to every line from textarea using PHP? I know we can get text from textarea to Variables and then? How to add enter- (new line) as variable?

Dever1
  • 13
  • 2

1 Answers1

0

This works:

<form method="GET">
<textarea name="inputFromTextArea">This is line 1
This is line 2
...
This is line 4</textarea>
<input type="submit">
</form>

<?php 

/* Convert text with lines to an array (\r\n = Carriage return & Newline on Windows machines*/
$lines = explode("\n", $_GET['inputFromTextArea']);

/* debug information to show the contents of $lines */
echo "<pre>";
print_r($lines);
echo "</pre>";

/* Loop over all lines and manipulate each line */
foreach($lines as $lineNr => $line){
    $lines[$lineNr] = trim($line,"\r") . " a suffix here "; // remove possible Carriage returns & concatenate stuff
}

/* debug information to show the contents of $lines */
echo "<pre>";
print_r($lines);
echo "</pre>";

?>

Read more on Carriage return & Newlines here: \r\n = Difference between \n and \r?

Community
  • 1
  • 1
Cagy79
  • 1,610
  • 1
  • 19
  • 25
  • 1
    When in doubt explode on `\n` and `ltrim($text,"\r")`. – apokryfos Feb 21 '17 at 13:53
  • Thank you very much :) I have a problem, because for me it doesn't work. I created – Dever1 Feb 21 '17 at 14:39
  • I would like to ask if there exist any other function to output a string without [n] =>? – Dever1 Feb 21 '17 at 17:28
  • replace it with nothing like this:str_replace("\n", "", $targetString); It replaces \n with nothing (empty brackets as 2nd parameter) – Cagy79 Feb 21 '17 at 20:12
  • I suppose, \n means new line. I had used str_replace but, nothing changed. I just want to delete numbers [0] => hello [1] => world – Dever1 Feb 22 '17 at 11:49
  • I need more info, you want to remove the KEY items from an array value or what do you mean? – Cagy79 Feb 23 '17 at 14:15