转义“
发布于 2024-10-14 11:15:36 字数 604 浏览 7 评论 0 原文

我试图做的(实际上可以,但只能在我的本地测试服务器上)是用 php 输出一个 php 文件。
问题似乎出在 php 的开始和结束标签上。
我在 php.net 中查找了 nowdoc 但找不到解决方案的线索。 如果我像这样使用它:

$fh = fopen($filenameandpath, 'w') or die("can't open file");
$stringData = <<<'WWR'
<?php
echo('test');
?>
WWR;
$suc = fwrite($fh, $stringData);
fclose($fh);

我会收到解析错误:

解析错误:语法错误,/home/ua000154/public_html/test.php 第 XX 行出现意外的 T_SL

此解析错误的行号始终是找到起始 php 标记的位置。我的问题似乎是我需要转义这个标签(我想我应该对结束标签做同样的事情)

我该怎么做?这实际上在我的测试服务器上有效,但一旦我将其上传到另一台服务器上的最终位置,它就不起作用了。

任何帮助表示赞赏。感谢您抽出时间!

What I was trying to do (and could, actually but only on my local testserver) is to output a php-file with php.
The problem seems to be the opening and closing tags of php.
I looked up nowdoc in php.net but could not find a clue to the solution.
If I use it like this:

$fh = fopen($filenameandpath, 'w') or die("can't open file");
$stringData = <<<'WWR'
<?php
echo('test');
?>
WWR;
$suc = fwrite($fh, $stringData);
fclose($fh);

I get a parsing error:

Parse error: syntax error, unexpected T_SL in /home/ua000154/public_html/test.php on line XX

The linenumber of this parsing error is always where the opening php-tag is found. My problem seems to be that I need to escape this tag (I presume I should do the same with the closing tag)

How could I do this? This actually worked on my test server but would not once I uploaded it to the final location on another server.

Any help is apreciated. Thanks you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2024-10-21 11:15:36

如果您有意使用NOWDOC 语法,请确保您的 PHP 服务器 正在运行 5.3 或更高版本,因为它是在那时引入的。 (您可以使用 phpinfo(); 进行检查)。这可以解释为什么它可以在您的开发服务器上运行,但不能在生产服务器上运行。

If you're intentionally using NOWDOC syntax, make sure your PHP server is running 5.3 or later, since that was when it was introduced. (You can check using phpinfo();). That would explain why it worked on your dev server but not on production.

╰ゝ天使的微笑 2024-10-21 11:15:36

好的,这是一个已经回答的老问题,但是这对于任何想要在早期版本的 php 中实现与NOWDOC 类似概念的人来说可能会有用。它使用输出缓冲从源文件中逐字捕获文本,无需任何变量解析等。即,如果您想插入变量,则没有太多用处,但您实际上可以在其中放入任何内容,只要它不包含字符“?>”,终止它。

请注意,它与使用 >>>TERMINATOR 和 >>>'TERMINATOR' 的 HEREDOC 和 NOWDOC 不同,因为变量是在文档之后定义的。

<?PHP

    function NOWDOC_() {
        ob_start();
    }

    function _NOWDOC(&$buf=false) {
        $buf_ = ob_get_contents();
        ob_end_clean();
        if ($buf!==false) $buf .= $buf_;
        return $buf_;
   }



   NOWDOC_(); ?>random garbage, not shown, but captured into $myvar 


   it has all sorts ] [* \%&  of characters in it

   and completely ignores things like {$this} or $_SERVER['REMOTE_ADDR';

   <?PHP _NOWDOC($myvar);





   NOWDOC_(); ?><HTML><HEAD></HEAD>

   <BODY>Here is some <B>nice</B> HTML & .

     <SCRIPT>

      alert("javascript!");

     </SCRIPT>  

           this also demonstrates using $var = _NOWDOC() syntax.


   </BODY>

   </HTML><?PHP $myhtml = _NOWDOC();


   echo "the html will be [".$myhtml."]";

?>

Ok, so this is an old question that has been answered, however this may be of use for anyone who wants to implement a similar concept to NOWDOC in earlier versions of php. it uses output buffering to capture text verbatim from the source file, without any variable parsing etc. ie not a lot of use if you WANT to insert variables, but you can literally put anything in it, as long as it does not include the characters "?>", which terminates it.

note that it differs from HEREDOC and NOWDOC that used >>>TERMINATOR and >>>'TERMINATOR' in that the variable is defined after the document.

<?PHP

    function NOWDOC_() {
        ob_start();
    }

    function _NOWDOC(&$buf=false) {
        $buf_ = ob_get_contents();
        ob_end_clean();
        if ($buf!==false) $buf .= $buf_;
        return $buf_;
   }



   NOWDOC_(); ?>random garbage, not shown, but captured into $myvar 


   it has all sorts ] [* \%&  of characters in it

   and completely ignores things like {$this} or $_SERVER['REMOTE_ADDR';

   <?PHP _NOWDOC($myvar);





   NOWDOC_(); ?><HTML><HEAD></HEAD>

   <BODY>Here is some <B>nice</B> HTML & .

     <SCRIPT>

      alert("javascript!");

     </SCRIPT>  

           this also demonstrates using $var = _NOWDOC() syntax.


   </BODY>

   </HTML><?PHP $myhtml = _NOWDOC();


   echo "the html will be [".$myhtml."]";

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文