0

自从我完成任何 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";?> 

我似乎遇到的问题是它将代码完美地附加到正确的位置,但显示 $title 而不是输入的标题,同样如此

我认为问题是你不能在另一个变量中包含一个变量,因为如果任何人都可以在表单中输入代码可能会很危险,有没有办法解决这个问题?我什至不需要严密的安全性,因为这将仅供内部使用。

成立

http://uk3.php.net/manual/en/function.eval.php 但无法很好地利用它。

谢谢

4

5 回答 5

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>';
于 2013-08-27T09:06:03.263 回答
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 函数也作为字符串。这真的没有任何意义。

于 2013-08-27T09:08:28.610 回答
0

编辑 XML 文档的最佳方法是使用 XML 解析器为您完成所有格式设置。就像您不是在二进制文件中而是在图像处理软件包中编辑图像一样。

幸运的是,有一些非常强大的 xml 库:你如何在 PHP 中解析和处理 HTML/XML?

这里还有另一个线程回答了“什么是最好的”问题:PHP 的最佳 XML 解析器

现在到你的问题。在 PHP 中有 2 种不同的字符串声明。有'符号和"符号。不同之处在于,当 PHP 出现 " 时,它会解析字符串并评估所有变量,而 ' 不会对字符串进行任何解析。例如:

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

我希望这很清楚?

于 2013-08-27T09:12:07.587 回答
0

欢迎回到 PHP 折叠。其他 2 个答案会让你继续前进,但为了让你知道你忘记了什么,我会添加这个。

单引号和双引号在 PHP 中的工作方式略有不同

如果将变量用双引号括起来,它将扩展该变量,而在单引号中则不会。所以 :-

$title = 'Hello';

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

$text = '$title World';   // $text = '$title World'
于 2013-08-27T09:18:22.110 回答
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)

于 2013-08-27T09:18:40.137 回答