$row[TemplateBody] = " Welcome to the Digital libraryYour login information is as follows: User Name: $Username Password: $Password ";
$Passwd = "hello";
$Usr = "Harsha";
str_replace(); how to replace the $Username and $Password with the values of $Passwd and $Usr;
String handling help
Moderator: General Moderators
You could use the eval() statement. Save the variable like this:
The single quotes will mean that the variables $Usr and $Passwd aren't replaced at that time, so they will be saved literally as "$Usr" and "$Passwd". Then later on you assign your $Passwd and $Usr variables, and then after that run the following bit of code:
Which will make it then evaluate the original string, but this time with the correct values.
Alternatively, you may want the simpler solution! In your original string just remove the dollar signs and make the words more unique, for example you could say "User Name: MyUserName Password: MyPassWord"; and then do:
Code: Select all
<?
$row[TemplateBody] = 'Welcome to the Digital libraryYour login information is as follows: User Name: $Usr Password: $Passwd ';
?>Code: Select all
<?
eval ("\$row[TemplateBody]= "$row[TemplateBody]";");
?>Alternatively, you may want the simpler solution! In your original string just remove the dollar signs and make the words more unique, for example you could say "User Name: MyUserName Password: MyPassWord"; and then do:
Code: Select all
<?
$row[TemplateBody] = ereg_replace("MyUserName", $Usr, ereg_replace("MyPassWord", $Passwd, $row[TemplateBody]));
?>It's as simple as:
If you read up at [php_man]str_replace[/php_man](), you would've found out about that. 
[big_search]string manipulation tutorial php[/big_search]
Code: Select all
<?php
$defined_username = "php_n00b";
$row['TemplateBody'] = str_replace ("{{username}}", $defined_username, $row['TemplateBody'] );
?>[big_search]string manipulation tutorial php[/big_search]
Last edited by m3mn0n on Tue Sep 07, 2004 5:35 am, edited 1 time in total.