2013-03-10 35 views
0

iam編寫一個php腳本,我希望該腳本執行,然後執行結果將被保存到另一個文件,然後它將重定向到該保存文件。在執行php腳本時回顯一個php代碼到html文件

我現在的問題是,IAM執行有一些PHP代碼,我需要保存,因爲它是在保存文件

<?php 
function ago($time) 
{ 
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 
    $lengths = array("60","60","24","7","4.35","12","10"); 

    $now = time(); 

     $difference  = $now - $time; 
     $tense   = "ago"; 

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 
     $difference /= $lengths[$j]; 
    } 

    $difference = round($difference); 

    if($difference != 1) { 
     $periods[$j].= "s"; 
    } 

    return "$difference $periods[$j] 'ago' "; 
} 
?> 

IAM嘗試從執行的腳本呼應這些行腳本,使上述行可以保存。 和iam這樣做。

<?php 
      function rowFromVar($last_modified) { 
       $result = " <?php $$last_modified = filemtime(__FILE__); 

       function ago($time) 
        { 
         $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 
         $lengths = array("60","60","24","7","4.35","12","10"); 

         $now = time(); 

          $difference  = $now - $time; 
          $tense   = "ago"; 

         for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 
          $difference /= $lengths[$j]; 
         } 

         $difference = round($difference); 

         if($difference != 1) { 
          $periods[$j].= "s"; 
         } 

         return "$difference $periods[$j] 'ago' "; 
        } 
        $last=ago($last_modified);"; 

    return $result; 

    } 
    print rowFromVar("last_modified"); 

但亞姆有一些錯誤,如

(!) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\SEO stat[Repica OF  server]\Tools\domain_lookup1.php on line 109 

任何幫助,將不勝感激。

+0

檢查語法。您只是犯了一個小錯誤,請參閱代碼突出顯示它破壞的位置。 – hakre 2013-03-10 14:02:38

+0

此外,nowdoc或heredoc字符串可能對您正在嘗試執行的操作更有用:http://php.net/string – hakre 2013-03-10 14:03:29

回答

0
<?php 
     function rowFromVar($last_modified) { 
      $result = <<<XXX_DELIMETER_XXX 
<?php \$last_modified = filemtime(__FILE__); 

      function ago(\$time) 
       { 
        \$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 
        \$lengths = array("60","60","24","7","4.35","12","10"); 

        \$now = time(); 

         \$difference  = \$now - \$time; 
         \$tense   = "ago"; 

        for(\$j = 0; \$difference >= \$lengths[\$j] && \$j < count(\$lengths)-1; \$j++) { 
         \$difference /= \$lengths[\$j]; 
        } 

        \$difference = round(\$difference); 

        if(\$difference != 1) { 
         \$periods[\$j].= "s"; 
        } 
XXX_DELIMETER_XXX; 
        return "$difference $periods[$j] 'ago' "; 
       } 
       $last=ago($last_modified);"; 

return $result; 

} 
print rowFromVar("last_modified"); 

它應該工作,和U使用轉義雙引號..

如果幫助,請使用+1按鈕

+0

輸出已保存,但變量名稱缺失。 >>。輸出就像<?php $ last_modified = filemtime(__ FILE__); (「second」,「minute」,「hour」,「day」,「week」,「month」,「year」,「decade」); = array(「60」,「60」,「24」,「7」,「4.35」,「12」,「10」); = time();它應該像$ periods = array(「second」,「minute」,「hour」,「day」,「week」,「month」,「year」,「decade」); $長度=數組(「60」,「60」,「24」,「7」,「4.35」,「12」,「10」); – 2013-03-10 14:43:00

+0

我再次編輯它...其實你必須輸入\ $當你想輸出**一個$符號...然後我偶然編輯了所有的$符號...如果你想要他們中的一些變量,只是刪除\之前... \像... \ $差= =現在 - $時間將簡單地搜索兩個變量$現在和$時間,將取代..這是我的錯,我沒有逃脫$ s ... – Ronnie 2013-03-10 14:49:05

+0

感謝您的幫助@羅尼。它確實像魅力一樣工作..非常感謝。 – 2013-03-10 16:33:01