Converting variable names in string to their values

Discussion in 'PHP' started by BigChase, Jul 26, 2006.

  1. #1
    How to convert variable names in string to their value when passing such strings to functions like echo and sendmail? For example:

    $user = 'Fred';
    $string = "Dear $user,<br><br>How are you today?";

    Want to create an echo statement that would display:

    Dear Fred,

    How are you today?​

    echo $string does not convert the '$user' contained within the string to the value of $user. Wish to do the same with the sendmail and email operations

    THank you.
     
    BigChase, Jul 26, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $user = "BigChase";
    $string = "Dear " . $user . ", there are several ways of doing this actually...";
    echo $string;
    //Or...
    $string = "Dear %s, this is using a function called sprintf().";
    $user = "Fred";
    echo sprintf($string, $user);
    //Or...
    $string = "Dear %s, how are you this fine %s?";
    echo sprintf($string, "Fred", "Thursday");
    
    PHP:
     
    T0PS3O, Jul 26, 2006 IP
  3. BigChase

    BigChase Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I need a solution that a) does not require reforming the initial string and b) does not assume we know the order that variables will appear in the string.
     
    BigChase, Jul 26, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't understand what you're on about.
     
    T0PS3O, Jul 26, 2006 IP
  5. BigChase

    BigChase Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Your solution requires reforming $string. Assume the value of $string is given to you arbitrarily be another function and could be:

    $string = "Dear &user, today is the $day of $month, $year. Your bill of $owed is due."

    OR

    $string = "Your $color coat of size $size is in the closet of $restaurant."

    We need a solution that would substitute the value for each of the variables contained within the string AFTER the string is formed.

    Thanks.
     
    BigChase, Jul 26, 2006 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I either still don't get it or it's plain impossible.

    If you a) don't know what string it's going to be and b) you don't know in which order the variables will be and c) you don't know the values of the variables then what possibly can it be you are trying to do?

    Maybe if you tell us how it fits in the bigger scheme of things you're trying to do we could come up with an alternative that does work.
     
    T0PS3O, Jul 26, 2006 IP
  7. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #7
    The problem is... your script DOES work.

    
    <?php
    $user = 'Fred';
    $string = "Dear $user,<br><br>How are you today?";
    
    echo $string;
    ?>
    
    Code (markup):
    See it in action: http://georgeboone.com/dev/user.php

    I was looking at your code and I didn't see anything wrong with it. I tried it and as you can see it does indeed work.

    - GB
     
    GeorgeB., Jul 26, 2006 IP
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    All the code in this thread 'works' but he is trying to do something fancy beyond the code's abilities.
     
    T0PS3O, Jul 26, 2006 IP
  9. BigChase

    BigChase Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Your script does work. It doesn't work, however, if the $string is given it's value from a fread() operation which is the case in our operation. Do you know why that is the case?
     
    BigChase, Jul 26, 2006 IP
  10. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #10
    If you define all those variables before you define $string. Then output $string.

    There is absolutely no reason it wouldn't work.
     
    GeorgeB., Jul 26, 2006 IP
  11. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #11
    I'd need to see the file contents you are reading with fread() to tell you that.
     
    GeorgeB., Jul 26, 2006 IP
  12. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #12
    So you mean you have a file with:

    Hello $dude, this is plan text.

    You fread it and do:

    $dude = "Fred";
    $string = file[0];
    echo $string;

    No, that wouldn't work because the $dude is a literal string in that file you're reading in. Something like this could work:

    $string = substr($file[0], '$dude', $dude);

    But then you'd still have to know it's called $dude inthe first place.

    Variable variables come closest to this but you'd need the inverse of it and I don't know whether it exists.

    You could search for all $'s, then search for the next space, replace all that with %s and use sprintf but you'd still need to know the order.

    Complicated stuff and beyond me at this time of night.
     
    T0PS3O, Jul 26, 2006 IP
  13. BigChase

    BigChase Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Doesn't work when string is defined by an fread() even if the referenced variables (eg, $user, etc.) are defined beforehand.
     
    BigChase, Jul 26, 2006 IP
  14. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #14
    Put the fread stuff in a function.

    Dump the function into a variable (ex $string = myFunctionName(); )

    echo $string;
     
    GeorgeB., Jul 26, 2006 IP
  15. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #15
    As per the PHP manual:

    Create a file called test.txt and put in the line:

    Please give me a cup of $drink if you don't mind.
    Code (markup):
    Create a separate PHP program as follows and run it:

    
    <?php
    $text = file_get_contents("test.txt");
    $drink = "coffee";
    echo $text, "\n";
    eval("\$text = \"$text\";");
    echo $text, "\n";
    exit;
    ?>
    
    Code (markup):
    It will write the following to the screen:
    Please give me a cup of $drink if you don't mind.
    Please give me a cup of coffee if you don't mind.

    At a minimum -- you must know what variables are likely to be contained in the file! Though undeclared variables should generate an empty result.
     
    clancey, Jul 26, 2006 IP
  16. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Nice one, forgot about that one...

    So depending on the source, it might still not be working...

    http://www.php.net/eval
     
    T0PS3O, Jul 26, 2006 IP
  17. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Maybe you need str_replace or something similiar?

    Let test.txt contain the following text:

    Can I have a cup of $drink ?

    First read the stuff from test.txt to $text. Then do this:
    
    $string = str_replace('$drink','coffee',$text);
    
    PHP:
    I think what you are try to do is sort of like an email templating system right? May be then you should try to use some other delimiter other than $. Like {FIRST_NAME} or something like that. Then in the code, make an array of the stuff you want to replace these tags with like
    
    $search = Array("{FIRST_NAME}","{LAST_NAME}","{USER}");
    $replace = Array("John",=>"Smith","john_smith");
    
    $text = "Hello Mr. {FIRST_NAME} {LAST_NAME}. Your username is {USER}";
    $message = str_replace($search,$replace,$text);
    
    PHP:
    Is that what you wanted?

    Thomas
     
    coderlinks, Jul 27, 2006 IP
  18. BigChase

    BigChase Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    GeorgeB, I'll try your suggestion; not sure if it'll work or not.

    Clancey, looks like the eval() function is the way to go, your caveats accepted.

    Coderlinks, you are correct that my application is an email templating system. str_replace with arrays works well, but requires knowing all the possible variable names in the template.

    Thanks all for your help.
     
    BigChase, Aug 2, 2006 IP
  19. elchuecodel88

    elchuecodel88 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Thanks all for your help. !!
     
    elchuecodel88, Aug 3, 2006 IP
  20. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Hey, you can use preg_match then.. to match everything between { } tags. Then you can compare them with an array of valid template tags and replace only those.

    Thomas
     
    coderlinks, Aug 3, 2006 IP