I was using the str_replace() function to replace some contents within a text file. My code:
$txt = file_get_contents('script.txt');
function makefile() {
$file = fopen("test.txt","w");
fwrite($file,"Hello World. Testing!");
fclose($file);
return "Done!";
}
$templates = array("{{time}}", "{{PI}}", "{{make}}");
$applied = array(date("h:i:sa"), 22/7, makefile());
$str = str_replace($templates, $applied , $txt);
echo $str;
The script.txt contains:
The time is {{time}} <br>
The value of PI is {{PI}}
As you can see, it's just a simple templating system. the makefile() function is used only for testing purpose. The script.txt file has no {{make}} templates. So normally, the makefile function won't need to be called during replacement operation. But when I run the code, it creates the test.txt. Which mean makefile() runs. Is there any way to avoid this kind of unnecessary function operations? And run them only when needed?
You would need to add strpos check. strpos in php is used to find the position a substring occurs in a string, but if the substring never occurs, then it will return false. Leveraging that, we can do:
<?php
$txt = "The time is {{time}} <br>
The value of PI is {{PI}}";
function makefile() {
$file = fopen("test.txt","w");
fwrite($file,"Hello World. Testing!");
fclose($file);
return "Done!";
}
$templates = array("{{time}}", "{{PI}}", "{{make}}");
$applied = array(date("h:i:sa"), 22/7, strpos($txt,"{{make}}") ? makefile() : false);
$str = str_replace($templates, $applied , $txt);
echo $str;
?>
Related
i searched Google but found nothing what fits for my problem, or i search with the wrong words.
In many threads i read, the smarty Template was the solution, but i dont wont use smarty because its to big for this little project.
My problem:
I got a CSV file, this file contents only HTML and PHP code, its a simple html template document the phpcode i use for generating dynamic imagelinks for example.
I want to read in this file (that works) but how can i handle the phpcode inside this file, because the phpcode shown up as they are. All variables i use in the CSV file still works and right.
Short Version
how to handle, print or echo phpcode in a CSV file.
thanks a lot,
and sorry for my Bad english
Formatting your comment above you have the following code:
$userdatei = fopen("selltemplate/template.txt","r");
while(!feof($userdatei)) {
$zeile = fgets($userdatei);
echo $zeile;
}
fclose($userdatei);
// so i read in the csv file and the content of csv file one line:
// src="<?php echo $bild1; ?>" ></a>
This is assuming $bild1 is defined somewhere else, but try using these functions in your while loop to parse and output your html/php:
$userdatei = fopen("selltemplate/template.txt","r");
while(!feof($userdatei)) {
$zeile = fgets($userdatei);
outputResults($zeile);
}
fclose($userdatei);
//-- $delims contains the delimiters for your $string. For example, you could use <?php and ?> instead of <?php and ?>
function parseString($string, $delims) {
$result = array();
//-- init delimiter vars
if (empty($delims)) {
$delims = array('<?php', '?>');
}
$start = $delims[0];
$end = $delims[1];
//-- where our delimiters start/end
$php_start = strpos($string, $start);
$php_end = strpos($string, $end) + strlen($end);
//-- where our php CODE starts/ends
$php_code_start = $php_start + strlen($start);
$php_code_end = strpos($string, $end);
//-- the non-php content before/after the php delimiters
$pre = substr($string, 0, $php_start);
$post = substr($string, $php_end);
$code_end = $php_code_end - $php_code_start;
$code = substr($string, $php_code_start, $code_end);
$result['pre'] = $pre;
$result['post'] = $post;
$result['code'] = $code;
return $result;
}
function outputResults($string) {
$result = parseString($string);
print $result['pre'];
eval($result['code']);
print $result['post'];
}
Having PHP code inside a CSV file that should be parsed and probably executed using eval sounds pretty dangerous to me.
If I get you right you just want to have dynamic parameters in your CSV file right? If thats the case and you don't want to implement an entire templating language ( like Mustache, Twig or Smarty ) into your application you could do a simple search and replace thing.
$string = "<img alt='{{myImageAlt}}' src='{{myImage}}' />";
$parameters = [
'myImageAlt' => 'company logo',
'myImage' => 'assets/images/logo.png'
];
foreach( $parameters as $key => $value )
{
$string = str_replace( '{{'.$key.'}}', $value, $string );
}
I'm running into an issue with finding specific text and replacing it with alternative text.
I'm testing my code below with .rtf and .txt files only. I'm also ensuring the files are writable, from within my server.
It's a hit and miss situation, and I'm curious if my code is wrong, or if this is just weirdness of opening and manipulating files.
<?php
$filelocation = '/tmp/demo.txt';
$firstname = 'John';
$lastname = 'Smith';
$output = file_get_contents($filelocation);
$output = str_replace('[[FIRSTNAME]]', $firstname, $output);
$output = str_replace('[[LASTNAME]]', $lastname, $output);
$output = str_replace('[[TODAY]]', date('F j, Y'), $output);
// rewrite file
file_put_contents($filelocation, $output);
?>
So, inside the demo.txt file I have about a full page of text with [[FIRSTNAME]], [[LASTNAME]], and [[TODAY]] scattered around.
It's hit and miss with the find/replace. So far, [[TODAY]] is always replaced correctly, while the names aren't.
Has anyone had this same issue?
(on a side note, I've checked error logs and so far no PHP warning/error is returned from opening the file, nor writing it)
Hard to say for sure without seeing the contents of demo.txt. My first guess is that it might be a problem with using brackets for your pointers. I would try changing to something not used by RTF like percent signs or an asterisk. ex: %%FIRSTNAME%%, **FIRSTNAME** (this is assuming of course that you have control of the contents of demo.txt.)
I have had this issue too. It seems like Microsoft Word inserts formatting codes in the tags. I have made a blog post about how to get around this on my technical blog.
http://tech.humlesite.eu/2017/01/13/using-regular-expression-to-merge-database-content-into-rich-text-format-template-documents/
The PHP example is shown here:
<?php
$file = file_get_contents('mergedoc.rtf');
// To temporary get rid of the escape characters...
$mergetext = str_replace("\\", "€€", $file);
// New seven part regex with default value detection
$regex2 = '/<<((?:€€[a-z0-9]*|\}|\{|\s)*)([a-z0-9.\-\+_æøåÆØÅA-Z]*)((?:€€[a-z0-9]*|\}|\{|\s)*)([a-z0-9.\-\+_æøåÆØÅA-Z]*)((?:€€[a-z0-9]*|\}|\{|\s)*)(?:\s*:(.*?)\s*)?((?:€€[a-z0-9]*|\}|\{|\s)*)>>/';
// Find all the matches in it....
preg_match_all($regex2,$mergetext, $out, PREG_SET_ORDER);
// Lets see the result
var_dump($out);
foreach ($out as $match) {
$whole_tag = $match[0]; // The part we actually replace.
$start = $match[1]; // The start formatting that has been injected in our tag, if any
$tag = $match[2]; // The tag word itself.
if (($match[4].$match[6]) != "") { //some sec-part tag or default value?
$end = $match[5]; // The end formatting that might be inserted.
if ($end == "") {
$end = $match[7]; // No end in 5, we try 7.
}
} else {
$end = $match[3]; // No second tag or default value, we find end in match-3
}
$secPartTag = $match[4]; // Do we have inserted some formatting inside the tag word too ?
if ($secPartTag != "") {
$tag .= $secPartTag; // Put it together with the tag word.
}
$default_value = $match[6];
// Simple selection of what we do with the tag.
switch ($tag) {
case 'COMPANY_NAME':
$txt = "MY MERGE COMPANY EXAMPLE LTD";
break;
case 'SOMEOTHERTAG':
$txt = "SOME OTHER TEXT XX";
break;
case 'THISHASDEFAULT':
$txt = "";
break;
default:
$txt = "NOTAG";
}
if ($txt == "") {
$txt = $default_value;
}
// Create RTF Line breaks in text, if any.
$txt = str_replace(chr(10), chr(10)."\\line", $txt);
// Do the replace in the file.
$mergetext = str_replace($whole_tag, $start.$txt.$end, $mergetext);
}
// Put back the escape characters.
$file = str_replace("€€", "\\", $mergetext);
// Save to file. Extention .doc makes it open in Word by default.
file_put_contents("ResultDoc.doc", $file);
?>
I need to practically generate a .PHP file using a PHP script or PHP command. The file generate will contain the following code:
<?php
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$lang_en="http://translate.google.com/translate_tts?tl=en&q={$text}";
$lang_ro="http://translate.google.com/translate_tts?tl=ro&q={$text}";
$lang_fr="http://translate.google.com/translate_tts?tl=fr&q={$text}";
$language=$lang_en;
$this->mp3data = file_get_contents($language);
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$en="http://translate.google.com/translate_tts?tl=en&q={$text}";
$ro="http://translate.google.com/translate_tts?tl=ro&q={$text}";
$fr="http://translate.google.com/translate_tts?tl=fr&q={$text}";
$lang=$en;
$this->mp3data = file_get_contents($lang);
return $this->mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
I want to do this because I want to have multiple choices regarding the language:
$language=$lang_en;
and
$lang=$en;
Not sure what your question is here. Generating a PHP file is no different than generating any other kind of file. Build a string of the source code you want to write, then a simple file_put_contents('filename.php', $contents); will do the trick.
Edit: Where are $en and $lang_en coming from? Also, could these not be passed as parameters to the function? Why must they be hard-coded?
$contents = '
<?php
// your PHP here
';
file_put_contents('filename.php', $contents);
You may also want to read up on heredocs for large blocks of text, but unfortunately those will replace your variables unless you escape them all with a backslash.
I have function build_additional_docs which calls another function that do few actions, but first it's call to function read_all_file, which extract the file to string variable and return it.
It's worked perfect when the function create_file_node has been called from another function.
but when it's called from build_additional_docs, the client wait to server untill time out...
I think that the function fail on fgets().
Additional comment: When I call function create_file_node whith with the same files, and the different is that file name is static string, and I have no foreach loop, the code works again...
here is my code:
function build_additional_docs($dir_name, $addDocsArr){
foreach ($addDocsArr as $doc) {
if($summery != ''){
$fileName = $dir_name . '\\' . $doc;
create_file_node($fileName);
}
}
function create_file_node($fileName){ global $base_url;
try{
$text = read_all_file($fileName);
}
catch (Exception $ex){
// some message here
}
return 0;
}
function read_all_file($file_name){
$file_handle = fopen($file_name, "r");
while (!feof($file_handle)) {
$line[] = fgets($file_handle);
}
fclose($file_handle);
return implode('',$line);
}
Found the mistake!
$addDocsArr variable is return value from explode() function for split string to seperated files names. The returned array include strings of file name with spacial characters that cannot be seen...
so when i add the code:
$fileName = $dir_name . '\\' . substr($doc, 0,strlen($doc) - 1);
the code worked.
I'm trying to get the contents of a PHP file after it is parsed, and then store it in a variable.
I couldn't get any useful information via Google, except for this one example:
ob_start();
include $file;
$content = ob_get_clean();
But this returns the contents as plain text, i.e.: The <?php and ?> tags are still there, and all code between the tags isn't parsed.
So I wanted to know, how can I do this properly?
update:
This is content of the file which is being included:
Testcontent
<?php echo 'This should be parsed, right?'; ?>
I was using this function several years ago for a sort of a template engine, it seems to do what you need - pass a string with some PHP code inside and it will return it with PHP executed. Surprisingly, it still works :-)
function process_php( $str )
{
$php_start = 0;
$tag = '<?php';
$endtag = '?>';
while(is_long($php_start = strpos($str, $tag, $php_start)))
{
$start_pos = $php_start + strlen($tag);
$end_pos = strpos($str, $endtag, $start_pos); //the 3rd param is to start searching from the starting tag - not to mix the ending tag of the 1st block if we want for the 2nd
if (!$end_pos) { echo "template: php code has no ending tag!", exit; }
$php_end = $end_pos + strlen($endtag);
$php_code = substr($str, $start_pos, $end_pos - $start_pos);
if( strtolower(substr($php_code, 0, 3)) == 'php' )
$php_code = substr($php_code, 3);
// before php code
$part1 = substr($str, 0, $php_start);
// here set the php output
ob_start();
eval($php_code);
$output = ob_get_contents();
ob_end_clean();
// after php code
$part2 = substr($str, $php_end, strlen($str));
$str = $part1 . $output . $part2;
}
return $str;
}
Based on Tyil's last comment he wants to entirety of the php file within a variable:
Tyil, what if my include file looks like this: <?php echo 'test stuff'; $foo = 'one';. What does $content contain and what happens if I try to access $foo from the including file?
#MikeB $content contains the string <?php echo 'test stuff'; $foo = 'one';. var_dump($foo); in the including file returns NULL.
<?php
$file = 'include.php';
$content = file_get_contents($file);
var_dump($content); // (string) "<?php echo 'This should be parsed, right?'; $foo = 'one'; ?>"
include.php:
<?php echo 'This should be parsed, right?'; $foo = 'one'; ?>
If you can modify your $file, than use return in it.
Otherwise cURL it (opens a web page like browser does).
This should definitely work. And it does for me:
[ghoti#pc ~/tmp]$ cat file1.php
#!/usr/local/bin/php
<?php
$file="file2.php";
include($file);
[ghoti#pc ~/tmp]$ cat file2.php
<?php echo "This should be parsed, right?\n"; ?>
[ghoti#pc ~/tmp]$ ./file1.php
This should be parsed, right?
[ghoti#pc ~/tmp]$
You might want to look at your included file (named in $file) and see if there is perhaps some strange character after the initial <?php that might cause it not to be interpreted as a PHP script.
To see a hex dump of what's in the file (so you can see what character actually follows <?php rather than what's displayed in your editor), use: od -c filename.php | less.
To do this, you have to use cURL.
Well, if you want to do it effectively, anyhow.
I know I'm several years late, but I was also looking for a solution to this issue, and I'd like to share the solution. Keep in mind, while this script does indeed get a PHP HTML page parsed, downloading via the web protocol is very slow, at least in my tests.
function GetHTMLPage($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
...don't thank me. Thank David Walsh. (https://davidwalsh.name/curl-download)