t_k_eoh Posted July 26, 2007 Share Posted July 26, 2007 Hi guys, I am a beginner and I hope you guys can help me... I need to change a sentence from an input text box into words and store it in a text file (file.txt) Eg. "Can someone pls help me" Can someone pls help me and then store all these into file.txt file Pls help me...thanks. Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/ Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 try <?php $filename = 'test.txt'; $somecontent = "Add this to the file\n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/#findComment-307693 Share on other sites More sharing options...
t_k_eoh Posted July 26, 2007 Author Share Posted July 26, 2007 Hi, I would like to know how to break a long sentence into words, eg. "can someone pls help me" into: can someone pls help me eg. using some sort of array method to store the words and then save it into a .txt file. Appreciate your help. Thanks Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/#findComment-307705 Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 try str_replace(' ', '</br>', $tring); or try \n instead of br Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/#findComment-307709 Share on other sites More sharing options...
DeadEvil Posted July 26, 2007 Share Posted July 26, 2007 <? $string = "can someone pls help me"; $str_pcs = explode(' ', $string); $str = ""; foreach($str_pcs as $word): $str .= $word."<br>"; endforeach; # create filename... * You can also put a dir. before the filename eg. "somedir/filename"; $txtfile = 'test.txt'; if (is_writable($txtfile )): if (!$handle = fopen($filename, 'w+')): echo "Cannot open file ".$txtfile; else: $opent_content = fopen($filename, 'w+'); $write_content = fwrite($filename, $str); if($write_content): @chmod($txtfile, 0777); echo "Success, wrote ".$str" to file ".$txtfile; else: echo "Cannot write to file ".$txtfile; endif; endif; fclose($handle); endif; ?> @teng84 try to read the question first!! (natuto ka nanaman sakin hahaha!) Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/#findComment-307794 Share on other sites More sharing options...
DeadEvil Posted July 26, 2007 Share Posted July 26, 2007 oops! changes this .. $opent_content = fopen($filename, 'w+'); $write_content = fwrite($filename, $str); to.. $open_content = fopen($filename, 'w+'); $write_content = fwrite(open_content, $str); Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/#findComment-307800 Share on other sites More sharing options...
t_k_eoh Posted July 26, 2007 Author Share Posted July 26, 2007 Hi DeadEvil, Thanks for the help but I found this message error: Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/content/p/e/n/penangcenter/html/word/test1.php on line 21 what's the reason? Pls advice... Thanks! Link to comment https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/#findComment-308127 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.