gpt4 book ai didi

PHP转义字符串

转载 作者:行者123 更新时间:2023-12-02 05:48:57 26 4
gpt4 key购买 nike

我正在调用一个传递如下字符串的函数

$templateProcessor->saveAs('C:\xampp\htdocs\portal\templates\doc1.docx');

问题是,我需要它更像
$templateProcessor->saveAs('C:\xampp\htdocs\portal\templates\' . $this->fileName . '.docx');

但是,如果我这样做,我的字符串就会断裂,我认为这是由于字符串中的反斜杠造成的。

我该如何解决这个问题?

附言我实际上更愿意做这样的事情
$templateProcessor->saveAs(dirname(__FILE__) . '../../templates/doc1.docx');

但它似乎不喜欢我使用 ../../因为我需要退出当前工作目录。

谢谢

最佳答案

要逃避反斜杠,只需在它们之前放置一个额外的反斜杠。

$fileName = 'file';
$path = 'C:\\xampp\\htdocs\\portal\\templates\\' . $fileName . '.docx';
print $path;

使 dirname工作,你可能只需要一个额外的斜线:
$path = dirname(__FILE__) . '/../../templates/doc1.docx';
print $path;

关于PHP转义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com