I have a text file with content as

END REPORT: report1
END REPORT: report2
END REPORT: report3

How can I read and Overwrite this text file such that "BEGIN REPORT: report3" is replaced with "BEGIN REPORT: <a href="#report3">report3</a> always.

Thanks a lot for your help


Recommended Answers

All 3 Replies

// first off, this does not overwrite 'report.txt', but you could do that.
// this creates a new file 'myreport.txt'
$file = 'report.txt';	
$newfile = 'myreport.txt';
$newhandle = fopen($newfile,"w");
$handle = fopen($file, "r");
//echo "parser processing file = " . $file . "\n";
while(!feof($handle)) {
	$data = fgets($handle);
// $listing, on the explode you may want to do both \r\n, it was difficult to format the resulting file when doing both.
	$listing = explode("\n", $data);
	// listing is an array of each line of data, so now go through each line.
	foreach ($listing as $line) {
		//echo "line =". $line."<br />";
// I initially had issues as \r appears to be 1 character when run through a counter.  
//you will need to adjust this to match the line you would like to overwrite.
		if ($line == "BEGIN REPORT: report3\r") {
			// like I said I had issues in resulting output, therfore the '; is on the second line so it was properly formatted for me
			// you can trial and error your way through creating your output file.
			$line = 'BEGIN REPORT: <a href="#report3">report3</a>
		if (fwrite($newhandle, $line) === FALSE) {
			//echo "there was an error writing to " . $newfile. "<br />";
		} else {
			//echo "successfully wrote " . $line . " <br />";
// close your file handles.

Thanks ddymacek.

Thanks a lot for the code, I just tried but looks like the line (#22) is not getting replaced in "myreport.txt".

Actually I want all the instances of this "BEGIN REPORT: report1" should be replaced with a hyperlink code i.e.BEGIN REPORT: <a href="#report1">report1</a> and similarly
BEGIN REPORT: report3 should be replaced as BEGIN REPORT: <a href="#report3">report3</a> and so on with the text file... how can I do that? Sorry I should have mentioned this earlier but some how missed it :(

you marked the thread a solved, but asked another question...
you would need to check for each individual line coming across,
something to the effect of, in your foreach statement:

if ($line == "BEGIN REPORT: report1\r") {
     $line = 'BEGIN REPORT: <a href="#report1">report1</a>
} else if ($line == "BEGIN REPORT: report2\r") {
     $line = 'BEGIN REPORT: <a href="#report2">report2</a>
} else if ($line == "BEGIN REPORT: report3\r") {
     $line = 'BEGIN REPORT: <a href="#report3">report3</a>

that should work for you..
again, some trial and error will be your approach as I do not now what terminates your lines. when I copied and pasted into a text file the line ended with \r\n.
I split them on the \n so I could read each line, and then tried to keep the file properly formatted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.