SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
-
Feb 27, 2009, 13:21 #1
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Problem Inserting Variables into Posted Text String
Hello All --
I have a problem with an email script I'm trying to implement. The script reads in a delimited txt file, runs it through an array, inserts values from the file such as name, date, card number into the email text and send the email to each email address read in from the text file. This code works quite well as is:
Code:$filestring = file_get_contents ('renewcards.txt'); $trimstring = trim ($filestring); $linearray = explode ("\n", $trimstring); $headers['Subject'] = 'Ann Arbor District Library card renewal'; foreach ($linearray as $mailarray) { $line = explode ("|", $mailarray); $exdatetrimmed = trim ($line [1]); $card = trim ($line [2]); $name = trim ($line [3]); $message = "Name: ".$name."\nCard: ".$card."\n\nYour library card from the xxx Library\r\nis due for renewal on ".$exdatetrimmed.". Please bring your \r\ncurrent picture I.D. and proof of address to the Circulation\r\nDesk at any xxx location \r\nto renew your card. "; $headers['To'] = $line[0]; $recipients = $line [0]; //$mail_object =& Mail::factory ("mail", $params); //$mail_object -> send($recipients, $headers, $message); if (mail($recipients, $subject, $message)) { echo ("Message Sent"); } else { echo "Message Delivery Failed."; } } } ?>
However, when I post the message through, it is one complete string and does not interpret my variables. I have split the string into an array and reassembled it as another string using something similar to this:
Code:if (isset($postedmessage)) { $message_pieces = array(); // array to store each if (is_int(strpos('"', $postedmessage))) { $message_pieces = explode('"', $postedmessage); // Multiple } else { $message_pieces[] = $postedmessage; // Just one } } foreach ($message_pieces as $returned_message) { $message_to_send = $message_to_send.$returned_message." "; }
So, I'm wondering, how I can rebuild this posted string and have my variables display correctly?
Thanks in advance for any help you can provide.
-
Feb 27, 2009, 14:16 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Use str_replace to replace the placeholders in the posted string with the variables.
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Feb 27, 2009, 14:27 #3
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 27, 2009, 14:35 #4
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
You don't need a foreach loop.
PHP Code:<?php
$aReplacements = array(
'{{RECIPIENT}}' => 'you@youraddress.com',
'{{SUBJECT}}' => 'A brief message from our sponsor.'
);
$sMessage = str_replace(array_keys($aReplacements), $aReplacements, 'This is a message to {{RECIPIENT}} with the subject "{{SUBJECT}}".');
echo $sMessage; #This is a message to you@youraddress.com with the subject "A brief message from our sponsor.".
?>@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
Feb 27, 2009, 14:45 #5
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Many Thanks, SilverBullet - I'll give that a try!
-
Feb 27, 2009, 15:05 #6
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
On second thought, I do have a follow up question...
At this point:
Code:$sMessage = str_replace(array_keys($aReplacements), $aReplacements, 'This is a message to {{RECIPIENT}} with the subject "{{SUBJECT}}".');
So I was thinking in order to rebuild it and do the str_replace, I would need to run it through a for each to do the str_replace and rebuild the string at the same time.
Am I correct in my thinking?
-
Feb 27, 2009, 15:10 #7
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
I thought you needed the placeholders so you didn't have to predefine the format? Are you now trying to define it?
I'm sorry, I don't follow.@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
Feb 27, 2009, 15:20 #8
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have a textfield for with javascript buttons that allow my users to set the placeholders anywhere they wish within the message. Once it is posted to my mailing page, it is one string.
I have the string itself and I have it split into an array using explode() and I have it rebuilt using something like this:
Code:if (isset($postedmessage)) { $message_pieces = array(); // array to store each if (is_int(strpos('"', $postedmessage))) { $message_pieces = explode('"', $postedmessage); // Multiple } else { $message_pieces[] = $postedmessage; // Just one } } foreach ($message_pieces as $returned_message) { $message_to_send = $message_to_send.$returned_message." "; } echo $message_to_send; }
Does that make any sense?
-
Feb 27, 2009, 15:30 #9
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
Yeah, starting to!
So why do you explode the string and create the array? Is there a need for this? Can you not just parse the string its entirety?@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
Feb 27, 2009, 15:38 #10
-
Feb 27, 2009, 15:42 #11
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
Feb 27, 2009, 18:45 #12
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So would this work?
PHP Code:
$filestring = file_get_contents ('renewcards.txt');
$trimstring = trim ($filestring);
$linearray = explode ("\n", $trimstring);
$headers['Subject'] = 'xxxLibrary card renewal';
foreach ($linearray as $mailarray)
{
$line = explode ("|", $mailarray);
$exdatetrimmed = trim ($line [1]);
$card = trim ($line [2]);
$name = trim ($line [3]);
$s = $postedmessage;
$r = array( '{{name}}' => $name,
'{{cardnum}}' => $card);
$s = strtr( $s, $r );
}
Thanks again, all, for your input!
-
Feb 27, 2009, 18:46 #13
- Join Date
- Jun 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So would this work?
PHP Code:
$filestring = file_get_contents ('renewcards.txt');
$trimstring = trim ($filestring);
$linearray = explode ("\n", $trimstring);
foreach ($linearray as $mailarray)
{
$line = explode ("|", $mailarray);
$exdatetrimmed = trim ($line [1]);
$card = trim ($line [2]);
$name = trim ($line [3]);
$s = $postedmessage;
$r = array( '{{name}}' => $name,
'{{cardnum}}' => $card);
$s = strtr( $s, $r );
}
Thanks again, all, for your input!
Bookmarks