【发布时间】:2018-01-07 01:42:49
【问题描述】:
我正在使用 heredoc 创建一个 php 文件,该文件需要在生成的文件中使用 heredoc,因为该文件将创建其他文件。
<<<EOD 位工作正常,我可以看到所有变量都已正确转义。
我遇到的问题是嵌套的heredoc <<<EOL 其中\$languageStrings 没有转义
如果我使用\$languageStrings并运行生成的文件ddd.php,输出是这样的:
<?php
= Array (
'LBL_LOCATION_INFORMATION' => 'Basic Information',
'LBL_CUSTOM_INFORMATION' => 'Custom Information',
'SINGLE_' => ''
);
如果我使用 \\$languageStrings 试图逃避我得到的反斜杠:
<?php
\ = Array (
'LBL_LOCATION_INFORMATION' => 'Basic Information',
'LBL_CUSTOM_INFORMATION' => 'Custom Information',
'SINGLE_' => ''
);
我怎样才能纠正这个问题,所以我得到$languageStrings = Array (?我确实考虑过尝试打开文件并插入特定的行,但文件总是不同的。
我愿意接受建议
这是我的heredoc的一个例子
$text = <<<EOD
This is some text
This is some more text
This is a \$variable //outputs this is a $variable
This is some more text
EOD;
$text .= <<<EOD
\n
\$targetpath = 'modules/' . \$moduleInstance->name;
if (!is_file(\$targetpath)) {
mkdir(\$targetpath);
mkdir(\$targetpath . '/language');
// create the language file
\$languagepath = 'languages/en_us';
\$languageContents = <<<EOL
<?php
\$languageStrings = Array (
'LBL_LOCATION_INFORMATION' => 'Basic Information',
'LBL_CUSTOM_INFORMATION' => 'Custom Information',
'SINGLE_\$moduleInstance->name' => '\$moduleInstance->name'
);
EOL;
file_put_contents(\$languagepath . '/' . \$module->name . '.php', \$languageContents);
}
EOD;
file_put_contents('ddd.php', $text);
【问题讨论】: