Jump to content

Recommended Posts

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.

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?

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]

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.

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);

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").

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.

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.