Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -- myFile.txt --
- Mary had a little $pet.
- // -- parser.php --
- $pet = "lamb";
- // open myFile.txt and transform it such that...
- $newContents = "Mary had a little lamb.";
- $allVars = get_defined_vars();
- echo $pet; // "lamb"
- echo $allVars['pet']; // "lamb"
- $text = 'this is a $test'; // single quotes to simulate getting it from a file
- $test = 'banana';
- $text = eval('return "' . addslashes($text) . '";');
- echo $text; // this is a banana
- $w+
- $allVars = get_defined_vars();
- $file = file_get_contents('myFile.txt');
- // unsure if you have to use single or double backslashes here for PHP to understand
- preg_replace_callback ("$w+", "find_replacements", $file);
- // replace callback function
- function find_replacements($match)
- {
- global $allVars;
- if (array_key_exists($match[0], $allVars)
- return $allVars[$match[0]];
- else
- return $match[0];
- }
- $allVars = get_defined_vars();
- $file = file_get_contents('myFile.txt');
- foreach ($allVars as $var => $val) {
- $file = preg_replace("@\$" . $var . "([^a-zA-Z_0-9x7f-xff]|$)@", $val . "\1", $file);
- }
- // -- myFile.txt --
- Mary had a little %pet%.
- // -- parser.php --
- $pet = "lamb";
- $fileName = myFile.txt
- $currentContents = file_get_contents($fileName);
- $newContents = str_replace('%pet%', $pet, $currentContents);
- // $newContents == 'Mary had a little lamb.'
- //myFile.txt
- Mary had a little <?= $pet ?>.
- //parser.php
- $pet = "lamb";
- ob_start();
- include("myFile.txt");
- $contents = ob_end_clean();
- echo $contents;
- Mary had a little lamb.
- $text = file_get_contents('/path/to/myFile.txt'); // "Mary had a little $pet."
- $allVars = get_defined_vars(); // array('pet' => 'lamb');
- $translate = array();
- foreach ($allVars as $key => $value) {
- $translate['$' . $key] = $value; // prepend '$' to vars to match text
- }
- // translate is now array('$pet' => 'lamb');
- $text = strtr($text, $translate);
- echo $text; // "Mary had a little lamb."
Add Comment
Please, Sign In to add comment