To replace variables in string with their values defined in php, you can use the strtr() or preg_replace_callback() function.

Example with strtr()

- Click to select it.
// array with data for the variables added in string
$data = array(
$str = 'Code-snippets from {$site} , added in: {$year}.';

$str2 = strtr($str, $data);
echo $str2;      // Code-snippets from , added in: 2014.

Example with preg_replace_callback()

- Click to select it.
// array with data for the variables added in string
$data = array(
$str = '<div id="{$id}">Parse this string, interpret {$domain} </div>';

$str2 = preg_replace_callback('/{\$([^}]+)}/', function($m) {
  GLOBAL $data;
  return $data[$m[1]];
}, $str);

echo $str2;      // <div id="test2">Parse this string, interpret </div>
- It is important to define data for variables into an Array with the keys that match the form you added the variable in string. Interpreting variables in string can also be useful to create and use template page; see this tutorial: Create and Use in PHP a Simple Template Page.

Daily Test with Code Example

Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong></strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert(""); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Interpret / Parse / Replace variable in string with value

Last accessed pages

  1. Mahjong Gardens (2458)
  2. Mystic India Pop (4182)
  3. Puzzle Games (7296)
  4. Zuma Deluxe (19371)
  5. SHA1 Encrypt data in JavaScript (3718)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49382)
  2. Read Excel file data in PHP - PhpExcelReader (37733)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (36749)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33176)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30541)