SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    use of variables within variables.

    Haven't tried can it be done? If I have a paragraph of text pulled from a db as $text, but that $text has other variables within it, $cat, $subcat, will the $cat and $subcat be parsed as values or not?

    Sketch
    Aaron Brazell
    Technosailor



  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand you correctly, I think they are.
    - the lid is off the maple syrup again!

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should be able to use the eval() function to print the $text variables which will plugin the other variables contained within $text. i think the following will work.

    PHP Code:
    eval("print $text;"); 

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but I'd have to use eval() where I actually want it printed, correct? My reason is for an email. I have a set of email templates and the templates simply have variables as place holders. So I'd use eval($var) as the body of the email, correct?

    Sketch
    Aaron Brazell
    Technosailor



  5. #5
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try it and see if it works.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  6. #6
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't work. Here's my code:

    PHP Code:
    $mailsql mysql_query("SELECT * FROM emails WHERE quickname = 'openaccount'");
            
    $mailrow mysql_fetch_array($mailsql);
                
    $sender    $mailrow['sender'];
                
    $subject$mailrow['subject'];
                
    $content$mailrow['content'];
            
    mail($custemail$subjectnl2br(eval('?>'.$content)), "F rom: ".$sender); 
    eval() prints the content of the email to the SCREEN and doesn't mail it.

    Any ideas?
    Sketch
    Last edited by Sketch; Jun 10, 2002 at 23:30.
    Aaron Brazell
    Technosailor



  7. #7
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
      mail($custemail$subjectnl2br($content), "From: QWOYIYZUN"); 
    doesn't this work? i don't really get what you're trying to do ... and the "Wrom:" header has not been in use since we found the 'edit' button

  8. #8
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vb changed From to Wrom and added all that crap afterwards in your quote. I edited above to make it display correctly...

    Anyways, am I being that unclear what I'm trying to do? I have a block of text, which is the body of an email, that is being pulled from the db. IT has placeholders, like $customer_fname, etc. I want the placeholders to be replaced by their values in the email--sent by the mail() function! eval() does NOT work. It displays everyting on the page and does NOT send an email...How can I parse the $content variable to have the variables therein replaced with their respective values?

    Sketch
    Aaron Brazell
    Technosailor



  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vb changed From to Wrom and added all that crap afterwards in your quote. I edited above to make it display correctly...
    sorry

    Anyways, am I being that unclear what I'm trying to do?
    no i'm being unclear in the head not understanding what you were trying to say... i get it now though

    you could try doing something like the 'below-mentioned'... use a return:
    PHP Code:
    <?php
    $name
    ='aaron';
    $content 'Sketch - $name';

    echo eval(
    "return \"$content\";");

    ?>

  10. #10
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    from my understanding of return (and understand, I will try this as soon as I can but I'm at work and can't right now), return immediately stops the processing of the eval(). So if return is right there at the beginning, wouldn't it be the equivalent of doing eval(""); since the eval never actually gets to see the $content due to the fact execution was halted. Wouldn't I be correct in that assumption?

    Sketch
    Aaron Brazell
    Technosailor



  11. #11
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so any other ideas on this by any of the PHPers here?

    Sketch
    Aaron Brazell
    Technosailor



  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe this will work, you can use the eval() function to store stuff in a variable as well

    PHP Code:
    $mailsql mysql_query("SELECT * FROM emails WHERE quickname = 'openaccount'");

    $mailrow mysql_fetch_array($mailsql);
    $sender $mailrow['sender'];
    $subject $mailrow['subject'];
    $content $mailrow['content'];

    eval(
    "\$message = \$content;");

    mail($custemail$subject$message'From: ' $sender); 

  13. #13
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Skip the eval() stuff. It's not php code you've got in the database it seems, but in fact something like a template. The $variables are just place holders which you'd replace with the content you want, remembering to the escape the $.

    For example, using your code, lets say there's a placeholder like $signature, used to add a signature to the email. You'd do something like this;

    PHP Code:
    $mailsql mysql_query("SELECT * FROM emails WHERE quickname = 'openaccount'");
            
    $signature "Visit Sitepoint";
            
    $mailrow mysql_fetch_array($mailsql);
                
    $sender    $mailrow['sender'];
                
    $subject$mailrow['subject'];
                
    $content$mailrow['content'];
                
    $contentstr_replace "\$signature"$signature$content );
            
    mail($custemail$subject$content"From: ".$sender); 
    If that doesn't put you on the right track, can you copy one of the dB entries can contains these "variables".

  14. #14
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Sketch
    from my understanding of return (and understand, I will try this as soon as I can but I'm at work and can't right now), return immediately stops the processing of the eval(). So if return is right there at the beginning, wouldn't it be the equivalent of doing eval(""); since the eval never actually gets to see the $content due to the fact execution was halted. Wouldn't I be correct in that assumption?
    no, using return is fine, and that's what i do so that eval() returns a value. return does immediately stop the processing of eval(), but in doing so, it returns whatever is after the return. if you were eval()ing the text

    return "Text!"; echo "Something";

    the echo would never be eval()d, but Text! would be returned. understand?


    also remember that you need to escape any double quotes and backslashes. here's how i take care of that:

    Code:
    // Text from database, etc.
    $txt = 'Hello "$name!"';
    
    // Prepare the text for eval() by adding return,
    // excaping things, and adding a ; at the end
    $txt = 'return "' . str_replace('"', '\"', str_replace('\\\', '\\\\\', $txt)) . '";';
    
    // $txt is now ready to be eval()d
    $name = 'Matt';
    
    // Should echo Hello "Matt!"
    echo eval($txt);
    Last edited by DR_LaRRY_PEpPeR; Jun 11, 2002 at 15:36.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  15. #15
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, kudos to HarryF. That solved my problem. Thank you, sir. And thanks to everyone else who lent a hand...stick around. I'll need you again, I'm sure.

    Sketch
    Aaron Brazell
    Technosailor



  16. #16
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so set me right on this: using return like i said would work fine too right

  17. #17
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, I have no idea. I didn't try. HarryF's worked so I stopped...

    Sketch
    Aaron Brazell
    Technosailor



  18. #18
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doc? i'm sure it worked... just run that snippet of code i posted... not that i'm trying to take anything away from HarryF but i just wanted to be sure why using a return wouldn't work...

  19. #19
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redemption
    Doc?
    yup, sure will. basically like mine. return definitely works, cuz i use it!

    if you're only doing one variable, HarryF's method is much faster than eval(). the problem is if you have many variables, multiple str_replace()s start to get slower (not to mention you have to have 1 for each variable). with eval() you can have as many variables in the string as you want, and it will work (as long as those variables are in the scope where eval() is being called).

  20. #20
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for clarifying doc!

  21. #21
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ::thinks about going back and using redemption's eval() version, but dreads reopening a can of worms after 3 days of agonizing work and finally getting it working::
    Aaron Brazell
    Technosailor




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
  •