TripleDES Posted May 31, 2007 Share Posted May 31, 2007 I can't seem to get heredoc to populate correctly with variables through a form. <textarea name="Template" rows="10" cols="80">Template Here</textarea> Contents could be something like: I want to replace $myarray[0] and another variable $myarray[1] I call heredoc this way: $hubarray = explode("\n", $contents); $template = $_POST['Template']; foreach ($hubarray as $val) { $hostarray = explode(",", $val); $output = <<<EOT $template EOT; I'm feeding it a file that has multiple rows separated by \n as array 1 and within those rows are fields separated by "," for array 2. I'm trying to iterate through the arrays and populate heredoc($template) accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/ Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 I think the question is, why are you using heredoc in this situation? (a) you're only making one variable equal to another (b) you don't really even need to do that. Heredoc is more useful for directly filling variables with long data containing lots of newlines and tabs. You copy $_POST['Template'] to $template to $output, but don't modify any of them along the way. Why not just use $_POST['Template']? Can you give an example of what you're trying to do in your last sentence? Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-265628 Share on other sites More sharing options...
TripleDES Posted May 31, 2007 Author Share Posted May 31, 2007 Heredoc will be basically a template that includes numerous whitespaces and variables. Here's an example: $output = <<<EOT set route $hostarray[0] gateway $hostarray[3] set route $hostarray[1] gateway $hostarray[3] set route $hostarray[2] gateway $hostarray[3] EOT; The uploaded fill will contain those variables: ipinfo.csv 1.1.1.0,1.1.1.1,1.1.1.2,1.1.1.3 2.2.2.0,2.2.2.1,2.2.2.2,2.2.2.3 3.3.3.0,3.3.3.1,3.3.3.2,3.3.3.3 I would like heredoc to be a variable. This way, I can input a different "template" into the text area. ie. set policy $hostarray[0] allow $hostarray[3] set policy $hostarray[1] allow $hostarray[3] set policy $hostarray[2] allow $hostarray[3] Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-265678 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Why not sprintf it? Template: set policy %1$s allow %4$s set policy %2$s allow %4$s set policy %3$s allow %4$s Code: $output = sprintf($_POST['Template'],explode(',',$val)); Since the template isn't in your code, but is supplied through the POST data, it just doesn't seem like a heredoc is for you. Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-265732 Share on other sites More sharing options...
TripleDES Posted May 31, 2007 Author Share Posted May 31, 2007 That doesn't seem to work as expected. I don't get any output and the $output returns boolean with a false value. $uploadfpath = "{$_FILES['userfile']['tmp_name']}"; $handle = fopen($uploadfpath, "rwb"); $template = $_POST['Template']; $hubarray = explode("\n", $contents $output = sprintf($template, $hubarray); var_dump($output); Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-265780 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Agh! (s)printf won't take an array in place of a list of values. Oh well. This worked for me: <?php $csvdata = "1.1.1.0,1.1.1.1,1.1.1.2,1.1.1.3\n2.2.2.0,2.2.2.1,2.2.2.2,2.2.2.3\n3.3.3.0,3.3.3.1,3.3.3.2,3.3.3.3"; $template = 'set policy %1$s allow %4$s' . "\n" . 'set policy %2$s allow %4$s' . "\n" . 'set policy %3$s allow %4$s' . "\n\n"; echo "csvdata:\n$csvdata\n\n"; echo "template:\n$template"; foreach (explode("\n",$csvdata) as $line) { $parts = explode(',',$line); printf($template,$parts[0],$parts[1],$parts[2],$parts[3]); } ?> :::OUTPUT::: csvdata: 1.1.1.0,1.1.1.1,1.1.1.2,1.1.1.3 2.2.2.0,2.2.2.1,2.2.2.2,2.2.2.3 3.3.3.0,3.3.3.1,3.3.3.2,3.3.3.3 template: set policy %1$s allow %4$s set policy %2$s allow %4$s set policy %3$s allow %4$s set policy 1.1.1.0 allow 1.1.1.3 set policy 1.1.1.1 allow 1.1.1.3 set policy 1.1.1.2 allow 1.1.1.3 set policy 2.2.2.0 allow 2.2.2.3 set policy 2.2.2.1 allow 2.2.2.3 set policy 2.2.2.2 allow 2.2.2.3 set policy 3.3.3.0 allow 3.3.3.3 set policy 3.3.3.1 allow 3.3.3.3 set policy 3.3.3.2 allow 3.3.3.3 Note that you must either use single quotes with the placeholders (i.e., '%1$s') or escape the dollar sign if using double quotes ("%1\$s"). Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-265810 Share on other sites More sharing options...
TripleDES Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks! It works with the following. <?php header("Cache-control: private"); $uploadfile = "{$_FILES['userfile']['name']}"; $uploadfpath = "{$_FILES['userfile']['tmp_name']}"; $handle = fopen($uploadfpath, "rwb"); $contents = fread($handle, filesize($uploadfpath)); fclose($handle); $hubarray = explode("\n", $contents); $template = $_POST['Template']; print "<pre>"; foreach ($hubarray as $line) { $line = trim($line); $parts = explode(",",$line); printf($template . "\r",$parts[0],$parts[1],$parts[2],$parts[3],$parts[4],$parts[5],$parts[6],$parts[7]); print "\r\r"; } ?> I just need to figure out a clever way to code $parts[0], $parts[1], etc. Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-265927 Share on other sites More sharing options...
Wildbug Posted June 1, 2007 Share Posted June 1, 2007 If you need a variable number of arguments to printf() and it won't take an array, you might consider writing the command on the fly and executing it via exec(). Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-266305 Share on other sites More sharing options...
TripleDES Posted June 15, 2007 Author Share Posted June 15, 2007 Since I was already using arrays for the parts, it was better to use vprintf. Now it works like a charm! Quote Link to comment https://forums.phpfreaks.com/topic/53682-solved-writing-to-heredoc-from-text-area-form/#findComment-275064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.