2013-08-27 37 views
0

自從我做了任何PHP編碼以來,已經很長時間了,所以我真的回到了原來的位置,如果我錯過了一些非常明顯/微不足道的事情,我會提前道歉。如何在變量中放置變量PHP

我想創建一個表單,它處理並提交數據並將一段代碼附加到XML文件的特定部分。

這是我有

<? $title = $_POST['title'] ; 
$author = $_POST['author']; 
$filesize = $_POST['filesize']; 
$pubdate = $_POST['pubdate']; 
$duration = $_POST['duration']; 
$summary = $_POST['summary']; 



$key = '<itunes:category text="Business"></itunes:category>'; 
$newline = '<item> 
<title>$title</title> 
<link>http://example.com/CS/podcast/<? php echo($target_path);?></link> 
<itunes:author>$author</itunes:author> 
<itunes:summary><?php echo($summary); ?></itunes:summary> 

<enclosure url="http://example.com/CS/$targetpath" length="$filesize" type="audio/mpeg"/> 

<guid>http://www.example.com/CS/podcast/<? php echo($target_path);?></guid> 
<pubDate><?php echo($pubdate);?></pubDate> 
<itunes:duration><?php echo($duration);?></itunes:duration> 
<itunes:keywords>My Podcast</itunes:keywords> 
<category>Podcasts</category> 
<itunes:explicit>no</itunes:explicit> 
</item>'; 


//copy file to prevent double entry 
$file = "list.xml"; 
$newfile = "listtemp.xml"; 
copy($file, $newfile) or exit("failed to copy $file"); 

//load file into $lines array 
$fc = fopen ($file, "r"); 
while (!feof ($fc)) 
{ 
$buffer = fgets($fc, 4096); 
$lines[] = $buffer;} 

fclose ($fc); 

//open same file and use "w" to clear file 
$f=fopen($newfile,"w") or die("couldn't open $file"); 

/* uncomment to debug 
print_r($lines); 
print "<br>\n"; 
*/ 

//loop through array using foreach 
foreach($lines as $line){ 
    fwrite($f,$line); //place $line back in file  
if (strstr($line,$key)){ //look for $key in each line 
fwrite($f,$newline."\n"); 
} //place $line back in file } 

fclose($f); 

copy($newfile, $file) or exit("failed to copy $newfile"); 
echo "done"; 
echo "$newline";?> 

我似乎是,它完美地追加代碼放到正確的地方,但顯示器$標題,而不是輸入標題同樣是真正的問題

我認爲問題在於你不能在另一個變量中有一個變量,因爲如果任何人都可以將代碼輸入到表單中,這是否有危險?我甚至不需要嚴密的安全措施,因爲這對於內部使用來說是不合理的。

找到

http://uk3.php.net/manual/en/function.eval.php 但未能將其善加利用。

感謝信

+0

如果我理解正確,您希望將**實際變量**寫入文件而不是其值? – MisterBla

+0

您並不需要頂部打開關閉php標籤,當你已經在PHP –

回答

1

嘗試:

$newline = '<item> 
<title>'.$title.'</title> 
<link>http://example.com/CS/podcast/'.$target_path.'</link> 
<itunes:author>'.$author.'</itunes:author> 
<itunes:summary>'.$summary.'</itunes:summary> 

<enclosure url="http://example.com/CS/'.$targetpath.'" length="'.$filesize.'" type="audio/mpeg"/> 

<guid>http://www.example.com/CS/podcast/'.$target_path.'</guid> 
<pubDate>'.$pubdate.'</pubDate> 
<itunes:duration>'.$duration.'</itunes:duration> 
<itunes:keywords>My Podcast</itunes:keywords> 
<category>Podcasts</category> 
<itunes:explicit>no</itunes:explicit> 
</item>'; 
+0

你是最好的人,非常感謝真棒輸入 – user2720867

0
$newline = '<item> 
<title>' . $title . '</title> 
<link>http://example.com/CS/podcast/' . $target_path . '</link> 
<itunes:author>' . $author . '</itunes:author> 
<itunes:summary>' . $summary . '</itunes:summary>'; 

等等 - 我覺得你得到的圖片。你寫它的方式,你告訴它,$ title是一個字符串 - 但它是一個變量。然後你把php標籤和echo函數作爲字符串。這沒有任何意義。

+0

好像利亞姆打敗了我.. :) – Kentora

0

編輯XML文檔的最佳方法是使用XML解析器爲您完成所有格式。 就像你不用二進制編輯圖像,但在圖像處理軟件包。

我們是幸運的,有一些非常強大的XML的圖書館在那裏:How do you parse and process HTML/XML in PHP?

還有這裏另外一個線程,回答了「什麼是最好的」問題:Best XML Parser for PHP

我們您的問題。在PHP中有2個不同的String聲明。有'表示法和"表示法。所不同的是,當PHP者隨「它解析字符串andevaluates所有變量,而」沒有做繩子的解析一個例子:?

$string = 'foo'; 
'my' . $string === 'myfoo'; 
'my$string' === 'my$string' !== 'myfoo'; 
"my$string" === 'myfoo'; 

我希望這是明確的

+0

看起來像利亞姆和肯托拉擊敗我:頁 – Pinoniq

0

歡迎回到PHP折。如果換其他2個回答將讓你去,但只是讓你知道你已經忘記了什麼,我會添加此。

單引號和雙引號的工作在PHP

略有不同雙引號中的變量pand這個變量,在單一quoutes它不會。所以: -

$title = 'Hello'; 

$text = "$title World"; // $text = 'Hello World' 

$text = '$title World'; // $text = '$title World' 
0

如果你不想用點('my var'。$ myvar。「酷」), 你可以只改變你「變成」

因此,這將是這樣的:

<?php 

    $inlineVar = "even this"; 
    $content = "This is a weird string! And look: '$inlineVar' works."; 
    $content .= "Stick an additonal string or something to it."; 

?> 

在前進:嘗試在你的PHP串不寫純HTML或XML。將它們展開(* .html,* .xml或* .phtml)