2016-09-25 37 views
0

我有一個函數:PHP的file_get_contents與變量

myfunc() { 
    $a = "John"; 
    $str = "Hello $a, how are you today?" 
    return $str; 
} 

echo myfunc(); //Hello John, how are you today? 

我想所有的sentenses保存在功能到另一個文件,因爲它們的長度太長,把它放在我的功能。

myfunc() { 
    $a = "John"; 
    $str = file_get_contents("inc_file.html"); 
    return $str; 
} 

inc_file.html:

Hello $a, how are you today? 

但它並沒有返回如我所料。變量$ a不會更改爲John。 請幫幫我! 謝謝

+0

不,我不希望保存文件,某些功能。我手動創建html文件。僅用於閱讀和顯示HTML文件內容的PHP代碼 –

回答

0

你可以包括.php文件,但機智.html不工作,因爲你包括HTML而不是PHP代碼。如果你把代碼和文件是這樣的:

<?php 
function myfunc() 
{ 
    $a = "John"; 
    $str = file_get_contents("inc_file.php"); 
    return $str; 
} 
?> 

inc_file.php:

Hello <?php echo $a; ?>, how are you today? 
+0

它沒有工作。我認爲是因爲函數「file_get_contents」不能傳遞變量。但是,如果我使用「包含」,而不是我的變量$ str –

+0

當我測試它工作 –

0

$str = file_get_contents("inc_file.html");線僅獲得文件內容。它未評估此內容和不會取代變量與它的值。

假設,會發生什麼,如果文件將是巨大的會有一些關於PHP變量的特定文本?

他們都應該被一些值取代?我想不是。所以,你只是有一個像$a這樣的符號的字符串。

如果你有一個字符串替換的東西 - 用str_replace,簡單的代碼是:

function myfunc() { 
    $a = "John"; 
    $str = file_get_contents("inc_file.html"); 
    return str_replace('$a', $a, $str); 
} 
0

你有什麼是好的,你可以只使用一個preg_replace_callback()或在這種情況下str_replace()做你想。只要做一些調整:

HTML:

"Hello ~val~, how are you today?" 

PHP

function myfunc ($a) 
    { 
     $str = str_replace('~val~',$a,file_get_contents("inc_file.html")); 
     return $str; 
    } 

echo myfunc('John'); 

簡單preg_replace_callback()例如:

function myfunc ($string,$a) 
    { 

     $str = preg_replace_callback('/(~[^~]{1,}~)/',function($v) use (&$a) 
      { 
       return array_shift($a); 
      },$string); 

     return $str; 
    } 

$string = "hello my name is ~name~. I come from ~city~"; 
echo myfunc($string,array('John','San Francisco')); 
1

所有這一切file_get_contents()確實是按照函數名稱的建議返回文件的內容。它不解析與" s中的字符串相關的內容。

對返回的文件內容使用str_replace()似乎達到你想要的。

$str = file_get_contents("inc_file.html"); 
$str = str_replace('$a', 'John', $str); 
return $str; 

如果你想更換多個「變量」你可以通過陣列str_replace,例如

$search = [$a, $b]; 
$replace = ['John', 'Brian']; 

$str = file_get_contents("inc_file.html"); 
$str = str_replace($search, $replace, $str); 
return $str; 

http://php.net/manual/en/function.str-replace.php

+0

沒有返回值沒關係。但是我的func中有很多變量。我之前在代碼中定義了所有這些變量。我必須重寫所有的str_replace函數?我真的希望它能自動檢測存在的變量而不需要替換。 –

0

下面是從加載的文件替換任何匹配變量的方法串。這是來自以下鏈接的代碼的修改版本。我修改它來檢查字符串類型。它假定變量是全局的。

How can I output this dynamic data without eval?

function interpolate($string){ 
foreach ($GLOBALS as $name => $value){ 
    // avoid array to string and object conversion errors 
    if(is_string($value)){ 
    $string = str_replace('$'.$name, $value, $string); 
    } 
} 
$string = preg_replace('/[$]\\w+/', '', $string); 
return $string; 
} 
$emailTemplate = file_get_contents('inc_file.html'); 
$emailTemplate = interpolate($emailTemplate); 
echo $emailTemplate; 

我發現這個像圖所示)尋找一種方式來做到這一點不用的eval(後:

$emailTemplate = file_get_contents('inc_file.html'); 
$emailTemplate = htmlentities($emailTemplate); 
eval("\$emailTemplate = \"$emailTemplate\";"); 
echo html_entity_decode($emailTemplate) ;