我有一个php readfile脚本,如下所示:
<?php
$contentFile = "http://google.com";
readfile( $contentFile );
?>`我想在readfile的输出中的特定行插入一段代码。
示例:
<html>
{top_code}
{Code i want to insert here}
{bottom-code}
</html>我如何才能做到这一点呢?
发布于 2012-07-19 22:54:48
你不能。readfile()会把你读到的东西传输到用户的浏览器上。您可以使用输出缓冲机制来捕获该数据,但是也可以使用file_get_contents(),这样就可以省去几行额外的代码。
file_get_contents以字符串的形式返回请求的文件/url。然后使用标准的string或DOM操作来操作这个“页面”。
发布于 2012-07-19 23:03:51
这可以完成这项工作
$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);发布于 2014-01-21 08:07:56
多亏了http://bavotasan.com/2010/display-rss-feed-with-php/,我发现这是使用PHP而不使用MYSQL的RSS feed问题的解决方案。
$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>';
}https://stackoverflow.com/questions/11563484
复制相似问题