duanping3587 2013-08-27 09:03
浏览 52

如何在变量PHP中放置变量

its been a long time since i've done any PHP coding so I'm really back to square one, apologies in advance if I am missing something really obvious/trivial.

I am trying to create a form, which processes and submits the data and appends a piece of code to a specific section of an XML file.

This is what I've got

<? $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>
";
*/

//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."
");
} //place $line back in file }

fclose($f);

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

The issue I seem to have is that it appends the code perfectly into the right place, but displays $title instead of the inputted title the same is true of

I think the issue is that you cannot have a variable within another variable, since it could be dangerous if anyone could just type code into forms, is there a way around this? I don't even need tight security as this would be soley for internal use.

Found

http://uk3.php.net/manual/en/function.eval.php but not been able to put it to good use.

Thank-you

  • 写回答

5条回答 默认 最新

  • douzi1986 2013-08-27 09:06
    关注

    try:

    $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>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘