Php code to open text, make an edit, and then close text file?

OK, so now I am feeling rather silly but I can not seem to write the code to open a text file via php, make the changes below and then close the file. I want to keep it simple so the

$text = substr($actualText, 0, -1);
$text.=’;’;

Is the way to go. Can someone give me the full code to open “crawl_info.txt” (which is my file) do the editing above and then close it out?

The code line above replaces the , with ; on the very last line of the text file.

<?php

$path = ‘crawl_info.txt’;

$contents = file_get_contents($path);
rtrim($contents);
$contents = substr($contents, 0, -1);

IF(SUBSTR($contents, -1) != ‘;’){$contents.= ‘;’;}

$fp = fopen($path, “w “);
fputs($fp, $contents);
fclose($fp);
?>

There are many ways to do it.. But the simplest:

$file = “location/to/file”;

$context = file_get_contents($file, FILE_USE_INCLUDE_PATH);

$text = substr($context, 0, -1);

file_put_contents($new_file_name, $text.”;”);

Leave a Reply