将php作为输出上的文本

时间:2017-03-01 20:48:57

标签: php html

首先这是我的PHP代码。我试图将此代码作为输出上的文本。不知道我是怎么做到的。使用html,我们可以\"将其插入到php中但是如何在我的代码中完成这样的操作?

<?php
$stringData = "

    // Start here

    <?php
    $width = $_GET['width'];
    $heigh = $_GET['height'];

        echo 'Hello';

    ?>
    // End here

    ";
?>

我已经标记了那个部分,我希望在文本输出中得到它,但当我把它放在页面上时,我得到语法错误,不知道为什么。

编辑#2

下面是我的完整代码,我解释了我的代码如何工作以及为什么。 我的代码是创建页面并在创建的页面中放置一些东西

<form method="post">
    <label>Page Name:</label><br>
    <input type='text' name='filename' placeholder='page name'>
    <label>Folders</label>
    <select name="thisfolder">
        <option value="">Default</option>
        <option value="Folder1">Folder1</option>
        <option value="Folder2">Folder2</option>
        <option value="Folder3">Folder3</option>
    </select><br><br>
    <label>content</label><br>
    <input type='text' name='strin' placeholder='content of created page'>
    <input type='submit' value='Add Feed'>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // the name of the file to create
    $filename=$_POST['filename'];
    // the name of the file to be in page created
    $strin=$_POST['strin'];
    // the name of the folder to put $filename in
    $thisFolder = $_POST['thisfolder'];
    // make sure #thisFolder of actually a folder
    if (!is_dir(__DIR__.'/'.$thisFolder)) {
        // if not, we need to make a new folder
        mkdir(__DIR__.'/'.$thisFolder);
    }
    // . . . /[folder name]/page[file name].php
    $myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php";
    $fh = fopen($myFile, 'w');
    $stringData = "

    <?php
    $width = $_GET['width'];
    $heigh = $_GET['height'];

        echo '';

    ?>

    ";

    fwrite($fh, $stringData);
    fclose($fh);
}
?>

我想要做的是,将$stringData内的php代码传递给将要创建的页面

6 个答案:

答案 0 :(得分:1)

您想要逃避文本中的$

像这样设置$stringData

$stringData = "

// Start here

<?php
\$width = \$_GET['width'];
\$heigh = \$_GET['height'];

    echo 'Hello';

?>
// End here

";

答案 1 :(得分:1)

使用highlight_string内部函数

echo highlight_string($stringData);

或使用htmlspecialchars

echo htmlspecialchars($stringData);

编辑,只要您不想将PHP代码直接打印到输出[正如您在评论中提到的那样]

这里的问题是你使用(双引号)来存储值,这在php中具有特殊含义

解决方案是将文本存储在单引号中,

<?php
$stringData = '

    // Start here

    <?php
    $width = $_GET["width"];
    $heigh = $_GET["height"];

        echo "Hello";

    ?>
    // End here

    ';
?>

答案 2 :(得分:1)

您使用的是双引号("),可让您在单引号($variables)不会使用的字符串中使用'

像这样:

$color = 'red';  

$string_one = "My car is $color."
echo $string_one; // My car is red.

$string_two = 'My car is $color.'
echo $string_two; // My car is $color.

因此,为了修复您的代码,您只需将双引号更改为单引号(并转义[在其他单引号之前加上反斜杠])。
像这样:

<?php
$stringData = '

    // Start here

    <?php
    $width = \$_GET[\'width\'];
    $heigh = \$_GET[\'height\'];

        echo \'Hello\';

    ?>
    // End here

    ';
?>

答案 3 :(得分:0)

您的代码

?>
    // End here must be in out put

    ";
?>

没有启动php dellimiter <?php

正确的是:

?>
    // End here must be in out put
<?php
    ";
?>

你又增加了一个php dellimiter,总计2:

<?php
$stringData = "
**?>**
    // Start here must be in out put
    <?php
    $width = $_GET['width'];
    $heigh = $_GET['height'];

        echo '';

    ?>
    // End here must be in out put
**<?php**
    ";
?>

答案 4 :(得分:0)

在你的代码中我添加了:

<?php
$stringData = '

    // Start here

    <?php
    $width = $_GET["width"];
    $heigh = $_GET["height"];

        echo "Hello";

    ?>
    // End here

    ';

    echo $stringData;
?>

当我打开这个phap页面时,我在浏览器中:

//从这里开始//在此结束

在页面源视图中我有:

// Start here

    <?php
    $width = $_GET["width"];
    $heigh = $_GET["height"];

        echo "Hello";

    ?>
    // End here

没有错误!我现在看到你不会的。

答案 5 :(得分:0)

此代码带&#34;(字符串)$ price&#34;工作:

<?php
$price = 10;

$stringData = "start here (string) $price end here";

echo $stringData;

echo "(string) $price";

?>
相关问题