sKunKbad Posted October 3, 2007 Share Posted October 3, 2007 I came up with this script the other night while working on another person's question here in this forum: <html> <head> <style type="text/css"> #editbox { width:500px; height:200px; } </style> </head> <body> <?php if ($_GET['editbox'] != ""){ $filename = 'fread_text.txt'; $somecontent = $_GET['editbox']; if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } }else{ $filename = "fread_text.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo "<form action='fread.php' method='get'> <input type='text' name='editbox' id='editbox' value='$contents'></input> <input type='submit' value='Submit' /> </form>"; fclose($handle); } ?> </body> </html> The script reads a file into a form's text area, allows a person to change that files text, and then writes the changed text back to the file when the submit button is pressed. My question is, how could I change only a selected div or paragraph of the file? For instance, if I wanted to only have the form read and change a paragraph whose ID is "paragraph1", how could I do that? Quote Link to comment https://forums.phpfreaks.com/topic/71730-read-a-files-or-by-id-then-replace-contents/ Share on other sites More sharing options...
sKunKbad Posted October 3, 2007 Author Share Posted October 3, 2007 I have been able to read in the text by parsing the html, but I still have not figured out how to write it back to the file. Here is the script to read it in: <?php $url = "file.txt"; $input = @file_get_contents($url) or die('Could not access file: $url'); $regexp = "<div id='somediv'>(.*)<\/div>"; if(preg_match_all("/$regexp/siU", $input, $matches)) { $string = $matches[1][0]; echo nl2br(htmlentities($string)); } ?> If anyone can help with the last half of this project, I'd appreciate it. I just need to know how to write back to the specific div (id="somediv"). Thanks for any help in advance. Quote Link to comment https://forums.phpfreaks.com/topic/71730-read-a-files-or-by-id-then-replace-contents/#findComment-361263 Share on other sites More sharing options...
sKunKbad Posted October 4, 2007 Author Share Posted October 4, 2007 Well, I've got this script that is working as I wanted it to work, but I'm pretty sure this isn't the most efficient way of doing this. Any help is still appreciated. I can't be the only person who has ever wanted to do something like this. Isn't this like the basics of a CMS or something? <html> <head> <style type="text/css"> #editbox { width:500px; height:200px; } </style> </head> <body> <?php if ($_GET['editbox'] != ""){ //if the editbox has anything in it $filename = 'html_parse_4_links.txt'; //this is the file to write to $edit = $_GET['editbox']; //this is the contents of the editbox $before = $_GET['before']; $after = $_GET['after']; $somecontent = urldecode($before); $somecontent.= "<div id='somediv'>"; $somecontent.= $edit; $somecontent.= "</div>"; $somecontent.= urldecode($after); // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In the example, they were opening $filename in append mode "a", but here we are overwriting the file in mode "w". //"a" mode might be pretty cool for a appending a blog with a user comment... if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } }else{ $url = "html_parse_4_links.txt"; $input = @file_get_contents($url) or die('Could not access file: $url'); $regexp = "(.*)<div id='somediv'>(.*)<\/div>(.*)"; if(preg_match_all("/$regexp/si", $input, $matches)) { $beforeEdit = urlencode($matches[1][0]); $string = $matches[2][0]; $theEdit = htmlentities($string); $afterEdit = urlencode($matches[3][0]); echo "<form action='html_parse_4_links.php' method='get'> <input type='text' name='editbox' id='editbox' value='$theEdit'></input> <input type='hidden' name='before' value='$beforeEdit'></input> <input type='hidden' name='after' value='$afterEdit'></input> <input type='submit' value='Save Back 2 File' /> </form>"; } } ?> </body> </html> and here is the text file I was working with (named html_parse_4_links.txt) what the heck <div id='somediv'>im ready for a pepsi</div> blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/71730-read-a-files-or-by-id-then-replace-contents/#findComment-361452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.