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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM