2013-08-27 36 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)