【问题标题】:insert specific element inside eod在 eod 中插入特定元素
【发布时间】:2020-03-02 09:56:12
【问题描述】:

安装时需要在文件中注入代码

结果必须是

if (isset($_SESSION['admin']['id'])) {
  define('DIR_FS_IMAGES', '{$dir_fs_document_root}images/');  
  define('DIR_WS_IMAGES', '{$http_catalog}images/'); 
}

在安装过程中,我尝试了这个,但似乎效果不佳。 如何处理才能得到与上面相同的结果?

谢谢

$file_contents = <<<ENDCFG
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}
ENDCFG;

【问题讨论】:

    标签: php heredoc


    【解决方案1】:

    您必须在 heredoc 字符串中转义美元符号字符 $,或者改用 nowdoc 字符串

    Nowdocs 是单引号字符串,就像 heredocs 是双引号字符串。 nowdoc 的指定与heredoc 类似,但在nowdoc 内部不进行解析。

    <?php
    $file_contents = <<<'ENDCFG'
    <?php
    if (isset($_SESSION['admin']['id'])) {
    define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
    define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
    }
    ENDCFG;
    
    file_put_contents('/tmp/out.php', $file_contents);
    

    结果

    $ php test.php
    $ file out.php
    out.php: PHP script text, ASCII text
    $ cat out.php
    <?php
    if (isset($_SESSION['admin']['id'])) {
    define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
    define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 2012-09-21
      相关资源
      最近更新 更多