Chen Chiu
Chen Chiu

Reputation: 47

PHP ReadFile Insert a code in readfile

I have a php readfile script, like this:

<?php
$contentFile = "http://google.com";
readfile( $contentFile );
?>`

I want to insert a code in a specific line in the output of the readfile.

Example:

<html>
{top_code}
{Code i want to insert here}
{bottom-code}
    </html>

How can I make this possible?

Upvotes: 0

Views: 329

Answers (3)

user3217173
user3217173

Reputation:

I found this, to be the solution to the problem of RSS feeds using PHP without MYSQL Thanks to 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(' & ', ' &amp; ', $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>';
}

Upvotes: 0

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

This can do the job

$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);

Upvotes: 0

Marc B
Marc B

Reputation: 360682

You can't. readfile() streams whatever you're reading out to the user's browser. You could use the output buffering mechanism to capture that data instead, but then you might as well just use file_get_contents() instead and save yourself a few extra lines of code.

file_get_contents returns the requested file/url as a string. Then you use standard string or DOM operations to manipulate that 'page'.

Upvotes: 2

Related Questions