带有 \n 的字符串导致以下变量错误

String with \n causing following variable errors

我有一个变量,其中包含 \n、空格和其他符号,并在我的代码中导致错误(注意:未定义的偏移量:0)。 我正在尝试通过 API 更新我的 MikroTik 路由器上的终端注释,但是当注释变量有多行和 \n 等等时,它会导致其余代码无法工作并给我错误。 我该如何解决这个问题?

我的代码:

$config['routeros']['note'] = "\
    \n ------------------------------------------------------------\
    \n Warning! This device is secured by\
    \n   _____                          ____                     _ \
    \n  / ____|                        |  _ \                   | |\
    \n | |  __  _ __  ___   ___  _ __  | |_) |  __ _  _ __    __| |\
    \n | | |_ || '__|/ _ \ / _ \| '_ \ |  _ <  / _` || '_ \  / _` |\
    \n | |__| || |  |  __/|  __/| | | || |_) || (_| || | | || (_| |\
    \n  \_____||_|   \___| \___||_| |_||____/  \__,_||_| |_| \__,_|\
    \n                                              N E T W O R K S\
    \n ------------------------------------------------------------\
    \n  \
    \n Unauthorised access is prohibited and shall render the user \
    \n liable to criminal and/or civil prosecution.";

PHP 文件的代码片段

print_r($config) 的输出;

使用 heredoc。示例:

config['routeros']['note'] = <<< END
    ------------------------------------------------------------
    Warning! This device is secured by\
       _____                          ____                     _ 
      / ____|                        |  _ \                   | |
     | |  __  _ __  ___   ___  _ __  | |_) |  __ _  _ __    __| |\
     | | |_ || '__|/ _ \ / _ \| '_ \ |  _ <  / _` || '_ \  / _` |
     | |__| || |  |  __/|  __/| | | || |_) || (_| || | | || (_| |\
      \_____||_|   \___| \___||_| |_||____/  \__,_||_| |_| \__,_|
                                                  N E T W O R K S
    ------------------------------------------------------------
    Unauthorised access is prohibited and shall render the user
    liable to criminal and/or civil prosecution.
END;

一直无法解决这个问题,因为没有人想修复它,所以我现在想出了一个解决方案。我创建了一个功能来检查我的路由器上的文件,如果存在则跳过但如果路由器上没有 sys-note.txt 文件,它将启用 ftp 服务(如果禁用),然后上传sys-note.txt 文件从我的服务器到路由器然后再次禁用 ftp 只有当它开始被禁用时。