PHP: Handy little Snippet for Multiline Text Box

This is a quick and dirty little snippet. I had a case where I had to fill in a Subject Line and a message with only one multiline text box. It wasn’t practical to have a hard coded subject line as it would be the same for every message or in my case bug.

Here is the full code.

<?
// Get the message String
$rawString = $_POST['comments'];

// Split the string into pieces for processing
$pieces = explode("\n", $rawString);

// First element is the subject line
$subject = $pieces[0];

// Take the array, delete the first entry, So we can pass it to $message
$messagePieces = array_slice($pieces, 1);

// Replace the \n or add a <br /> if you like.
$message = implode("<br />", $messagePieces);
echo "Subject: ". $subject;
echo "<br />";
echo "Message: ". $message;
?>
<form action="<? echo $_SERVER['php_self'] ?>" method="post">
	<textarea id="comments" name="comments">Your message</textarea>
	<input name="send" type="submit" value="Send" />
</form>

Not a lot going on here. I simply took the comments and assigned them to $rawString. The new line character is \n and are going to be on every new line (when you hit return) I can use the PHP Function explode() with our $rawString variable.

$pieces = explode("\n", $rawString);

This will give us an array to work with. Our subject is now $subject = $pieces[0];. Now we need to rebuild the message but we don’t want to loop through it printing each line separately. We can use another PHP Function called array_slice(), This will cut out the first row of the array, the Subject line.

$messagePieces = array_slice($pieces, 1);

Now we cram it all back together using PHP implode(), this little guy will rebuild the array. I decided to parse it with a
tag instead of replacing the /n, you could set this to whatever you like. But in this context we are going to use the
tag so the message will display correctly as HTML.

$message = implode("<br />", $messagePieces);

One Message box and we now have our Subject line and message. You can adapt this to do many things.

Enjoy!

5 Responses to PHP: Handy little Snippet for Multiline Text Box



    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'vision_comment' not found or invalid function name in /home/adamdev/adampatterson.ca/wp-includes/comment-template.php on line 1334

    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'vision_comment' not found or invalid function name in /home/adamdev/adampatterson.ca/wp-includes/comment-template.php on line 1334

    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'vision_comment' not found or invalid function name in /home/adamdev/adampatterson.ca/wp-includes/comment-template.php on line 1334

    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'vision_comment' not found or invalid function name in /home/adamdev/adampatterson.ca/wp-includes/comment-template.php on line 1334

    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'vision_comment' not found or invalid function name in /home/adamdev/adampatterson.ca/wp-includes/comment-template.php on line 1334

Leave a Comment