Posted By

adampatterson on 09/09/10


Tagged

php array explode slice implode


Versions (?)

PHP: Handy little Snippet for Multiline Text Box


 / Published in: PHP
 

URL: http://www.adampatterson.ca/blog/2010/09/php-handy-little-snippet-for-multiline-text-box/

  1. <?
  2. // Get the message String
  3. $rawString = $_POST['comments'];
  4.  
  5. // Split the string into pieces for processing
  6. $pieces = explode("\n", $rawString);
  7.  
  8. // First element is the subject line
  9. $subject = $pieces[0];
  10.  
  11. // Take the array, delete the first entry, So we can pass it to $message
  12. $messagePieces = array_slice($pieces, 1);
  13.  
  14. // Replace the \n or add a <br /> if you like.
  15. $message = implode("<br />", $messagePieces);
  16. echo "Subject: ". $subject;
  17. echo "<br />";
  18. echo "Message: ". $message;
  19. ?>
  20. <form action="<? echo $_SERVER['php_self'] ?>" method="post">
  21. <textarea id="comments" name="comments">Your message</textarea>
  22. <input name="send" type="submit" value="Send" />
  23. </form>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: cigraphics on September 11, 2010

There is already a function called nl2br http://php.net/nl2br

You need to login to post a comment.