2009-07-12 50 views

回答

4

You should try the steps located here。這些是將任何返回代碼的腳本集成回編輯器的一般步驟。

注意在第5步:

<?php 

$f = fopen("php://stdin", "r"); 
$s = fread($f, 100000); // should be big enough 
fclose($f); 
echo "\"" . $s . "\""; 

?> 

這應該被忽略,是非常草率的。它會像其他PHP腳本posted here的格式。

現在來看看如何實際使用PHP_beautifier,refer to the documentation

引述文件:

// Create the instance 
    $oBeautifier = new PHP_Beautifier(); 

    /* snip optional stuff*/ 

    // Define the input file 
    $oBeautifier->setInputFile(__FILE__); 

    // Define an output file. 
    // $oBeautifier->setOutputFile(__FILE__.'.beautified.php'); No need for this 

    // Process the file. DON'T FORGET TO USE IT 
    $oBeautifier->process(); 

    // Show the file (echo to screen) 
    $oBeautifier->show(); 

    // Save the file 
    //$oBeautifier->save(); No Need for this. 

OK,所以我們需要給它一個文件,而不是,但我看了在主Beautifier.php文件,它似乎接受STDIN以某種方式。因此,讓腳本:

<?php 

class BeautifyCode 
{ 

    public function run() 
    { 
     require_once('path/to/Beautifier.php'); // It's the main file in the PEAR package 

     // Create the instance 
     $oBeautifier = new PHP_Beautifier(); 

     // Define the input file 
     // I believe you leave blank for STDIN, looking through the code ** 
     $oBeautifier->setInputFile(); 
     // If that doesn't work try: 
     // $oBeautifier->setInputFile('php://stdin'); 

     $oBeautifier->process(); 

     $oBeautifier->show(); 

     // If that doesn't work, try this: 
     // echo utf8_decode($oBeautifier->get()); 
    } 

} 

$bc = new BeautifyCode; 
$bc->run(); 

?> 

所以這個保存爲一個PHP文件的任何地方,然後按照步驟第一個鏈接的3調用它。我會安全並使用@[email protected],因爲PHP_beautifier可能需要它。

我很抱歉,我不完全確定PHP_beautifier如何處理STDIN輸入。我查看了代碼,但無法確定。另一種選擇是始終保存首先清理的PHP文件,然後查看phpED文檔,瞭解如何獲取已打開並正在清理的PHP文件的路徑。

如果我有更多的時間瀏覽PHP_beautifier包,我可以給出一個更明確的答案。

+0

感謝。 我遇到了路徑問題。我的美化者存儲在「D:\ PHP_Beautifier \ Beautifier.php」 如何在require_once('path/to/Beautifier.php')輸入路徑; – 2009-07-12 15:02:25

0

您可以使用標準輸入和標準輸出爲輸入或輸出

// Create the instance 
    $oBeautifier = new PHP_Beautifier(); 

    // Define the input file 
    // I believe you leave blank for STDIN, looking through the code ** 
    $oBeautifier->setInputFile(STDIN); 
    $oBeautifier->setOutputFile(STDOUT); 
    $oBeaut->process(); 
    $oBeaut->save();