elis Posted April 20, 2009 Share Posted April 20, 2009 I haven't actually written the code for this yet, since I haven't figured out how to do what the title says: but, I have a file "header.php" that contains a line (amongst two hundred other lines) <php require(include.php); ?> I'm trying to use fwrite(); in another file that can alter this line, and only this line, based on what a user enters into a form. However, I can't figure out how to access the specific line in "header.php" I've read of fseek(); on PHP.net and some other sites, but don't quite understand it's purpose or if it would aid in my goal. Could someone recommend how I could go about this? Link to comment https://forums.phpfreaks.com/topic/154864-solved-need-help-using-fwrite-to-overwrite-a-specific-linetext-in-a-file/ Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 This should work: $fileurl = 'file url here'; $search = '<php require(include.php); ?>'; $replace = 'what you want there instead'; $file_contents = file_get_contents($fileurl); $fh = fopen($fileurl, "w"); $file_contents = str_replace($search,$replace,$file_contents); fwrite($fh, $file_contents); fclose($fh); To be sure not to overwrite something else, add some specific comment lines to the code. IE: <php require(include.php); /* special */ ?> - that way it's not the same as any other piece of code. Or you could also go by line, but I don't have a handy code ready for that. Link to comment https://forums.phpfreaks.com/topic/154864-solved-need-help-using-fwrite-to-overwrite-a-specific-linetext-in-a-file/#findComment-814479 Share on other sites More sharing options...
elis Posted April 20, 2009 Author Share Posted April 20, 2009 Thank you very much. That worked perfectly. Link to comment https://forums.phpfreaks.com/topic/154864-solved-need-help-using-fwrite-to-overwrite-a-specific-linetext-in-a-file/#findComment-814484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.