2017-03-01 114 views
0

首先這是我的PHP代碼。我試圖將此代碼作爲文本輸出。不知道我該怎麼做。與HTML我們可以做\"插入PHP內,但我如何得到這樣做就像我的代碼?獲得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); 
} 
?> 

什麼,我試圖做的是頁面內,通過在PHP代碼裏面$stringData到將要創建

+0

你不呼應的變量在那裏,但如果你沒有'的eval($ StringData是) ;' – AbraCadaver

回答

1

你想在你的文字逃離$的。

設置$stringData這樣的:

$stringData = " 

// Start here 

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

    echo 'Hello'; 

?> 
// End here 

"; 
+0

你可以請ecit我的代碼,並告訴我如何?在你的回答 –

+0

代碼與php星和關閉標籤之間的開始這裏和結束在這裏必須作爲文本 –

+0

只是,如果它的工作,請將其設置爲接受。謝謝! – JediBurrell

0

你的代碼頁

?> 
    // End here must be in out put 

    "; 
?> 

dellimiter <?php

正確的是有沒有開始PHP代碼:

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

您mised一個更多的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** 
    "; 
?> 
+0

我更新的代碼,你看它說,從這裏開始 - 這裏結束 –

+0

有代碼之間從這裏開始到這裏結束 –

+0

看起來親愛的。我不知道很多在PHP中,但這個'$ stringData'稱爲可變權? –

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 

    '; 
?> 
+0

親愛的編輯我的代碼在你的答案,所以我可以理解。我不太瞭解php –

+0

@MmPp你想打印用'$ stringData'字符串寫成的php代碼作爲純文本輸出? – hassan

+0

請檢查我的問題我已經添加編輯#2完整代碼 –

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 

沒有錯誤!我現在看到你會做什麼。

+0

看看eval這裏http://php.net/manual/en/function.eval.php – b2ok

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 

    '; 
?> 
+0

非常感謝你的時間和回答,但@JediBurrell已經修復了它。是的,因爲你提到'逃避[把反斜槓之前]'這是問題 –

+0

很高興你明白了! –

+0

我剛剛與解釋bro聊天時發佈了代碼 –

0

此代碼 「(字符串)$價格」 的工作:

<?php 
$price = 10; 

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

echo $stringData; 

echo "(string) $price"; 

?>