SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    SitePoint Enthusiast
    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.";
    }
    
    }
    }
    ?>
    My problem is I do not want to build the message here. I want to give my users the ability to build the email themselves in a form that posts to this page AND determine where the person's name, expiration date and other dynamic info appear in the message. I have javascript buttons on this form that allow them to do this - and it inserts the PHP code equivalent of the variables I use on the mailing portion.

    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." ";
    }
    The result of this, of course, is another string.

    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.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use str_replace to replace the placeholders in the posted string with the variables.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Use str_replace to replace the placeholders in the posted string with the variables.
    I did see that as a possible option, but I was not sure how to set that up within the context of the foreach loop...

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many Thanks, SilverBullet - I'll give that a try!

  6. #6
    SitePoint Enthusiast
    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}}".');
    I have the text of my email already posted to the page as a variable, and I have it in its own array.

    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?

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    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.".
    ?>
    PHP Code:
    $s 'This is a message to {{RECIPIENT}} with the subject "{{SUBJECT}}".'
    $r = array(
        
    '{{RECIPIENT}}' => 'you@youraddress.com',
        
    '{{SUBJECT}}' => 'A brief message from our sponsor.'
    );

    $s strtr$s$r ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    $s 'This is a message to {{RECIPIENT}} with the subject "{{SUBJECT}}".'
    $r = array(
        
    '{{RECIPIENT}}' => 'you@youraddress.com',
        
    '{{SUBJECT}}' => 'A brief message from our sponsor.'
    );

    $s strtr$s$r ); 
    OK OK, I'll give you this one!

    Nice work.
    @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.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    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.

  10. #10
    SitePoint Enthusiast
    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;
      
      }
    Your example had the text spelled out in a string already. I'm trying to figure out how to do the str_replace within a foreach that rebuilds my text string from an array.

    Does that make any sense?

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    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.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    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?

    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 );


    I have to take it through the foreach loop because its doing this for every line of my text file. $postedmessage is the full string posted from my form page.

    Thanks again, all, for your input!

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    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?

    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 );


    I have to take it through the foreach loop because its doing this for every line of my text file. $postedmessage is the full string posted from my form page.

    Thanks again, all, for your input!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •