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 multi line text box. It wasn’t practice to have a hard coded subject line as it would be the same for every message or in my base bug. Have a look at the code; I will explain what is going on below.
[sourcecode language='php']
<? // 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>
[/sourcecode] Not a lot of craziness going on here. I simple took the comments and assigned them to $rawString. Since I know that the new line cheracters n are going to be on every new line i can use the PHP Function explode() with our $rawString variable.
$pieces = explode("n", $rawString);
This will give us an array as well as some material to work with. Our subject is now $subject = $pieces[0];. Now we need to rebuild the message as we don’t want to loop through it printing each line separately. We use another PHP Function called array_slice() and we will cut out the first row of the array, the Subject line.
$messagePieces = array_slice($pieces, 1);
Now we can cram it all back together using PHP implode(), this little guy will take an array and rebuild it. I decided to parse it with a
tag instread of replacing the /n, you could set this to whatever you like. But in this context we are going to use the
tag.
$message = implode("<br />", $messagePieces);
Thats it. One Message box and we now have our Subject line and message. You can adapt this to do many things. Enjoy!