我有一个 php readfile 脚本,如下所示:
<?php
$contentFile = "http://google.com";
readfile( $contentFile );
?>`
我想在 readfile 的输出中的特定行中插入代码。
例子:
<html>
{top_code}
{Code i want to insert here}
{bottom-code}
</html>
我怎样才能做到这一点?
你不能。readfile()
将您正在读取的任何内容流式传输到用户的浏览器。您可以改用输出缓冲机制来捕获该数据,但是您也可以file_get_contents()
改用它并为自己节省几行额外的代码。
file_get_contents 将请求的文件/url 作为字符串返回。然后,您使用标准字符串或DOM操作来操作该“页面”。
我发现这是解决使用 PHP 而不使用 MYSQL 的 RSS 提要问题的解决方案,感谢http://bavotasan.com/2010/display-rss-feed-with-php/
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
这可以完成工作
$contentFile = "http://google.com";
$html = file_get_contents($contentFile);
$html = explode("\n",$html);
$line = $line_number - 1;
array_splice($html, $line, 0,"Burim Shala");
$html = implode("\n",$html);