【发布时间】:2018-08-24 16:26:13
【问题描述】:
我有一个变量,其中包含\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.";
print_r($config) 的输出;
【问题讨论】:
-
这段摘录没有语法错误。尽管如此,为什么不使用 HEREDOC?
-
那是什么,我该怎么做?
-
见"php manual strings heredoc"。您应该就具体问题进行更多解释。没有上下文,未定义的索引错误及其与该字符串的关系是不清楚的。也不清楚为什么它包含真正的换行符的额外转义。 你是怎么得出这个结论的?
-
错误来自note变量后面的变量。在我看来,ASCII 字符似乎在结束之前以某种方式关闭了变量字符串引号,然后导致我的代码的其余变量出错
-
如果后续行看起来相关,您应该include them。否则,恐怕这可能会无人回答。