mladja04 Posted December 4, 2010 Share Posted December 4, 2010 Hello, I am novice in PHP, I read book and try to write some PHP code, but I don't have success in that my attempt. Do someone can to help me? I think that this what I work isn't hard, but I don't know to do. I need to create in PHP some code which will: 1. In text field I enter some words in 2-3 rows, example word11 word12 word13 word21 word22 word23 word31 word32 word33 and click to button Convert, 2. After click on that button php script will take that words and convert into this mode: word11-word12-word13(TAB here without space)word11 word12 word23 word21-word22-word23(TAB here without space)word21 word22 word23 word31-word32-word33(TAB here without space)word31 word32 word33 (spaces between words will convert into - and between first and second mode with TAB) 3. On that way converted text to be saved into new file which will be with name of first line "word11 word12 word13". I know procedure to make this, but I don't know syntax very well Can someone write me code or part of code, to give me idea how I to make this? Thank you very much, Mladen Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted December 4, 2010 Share Posted December 4, 2010 this is very basic, if you are taking user input you should probably add some field validation but here you go: <?php $strOutPut = ""; if(isset($_POST['txtInput']) && trim($_POST['txtInput']) != "") { $arrLines = explode("\n", str_replace("\r", "", $_POST['txtInput'])); array_walk($arrLines, "trim"); foreach($arrLines as &$value) { $strOutPut .= str_replace(" ", "-", $value) . "\t" . $value . "\n"; } $strOutPut = substr($strOutPut, 0, -1); $fp = fopen('path/to/file.txt', 'w'); fwrite($fp, $strOutPut); fclose($fp); } ?> <html> <head> </head> <body> <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <textarea name="txtInput" style="width:500px; height:300px;"><?php echo trim($strOutPut) != ""?$strOutPut:""; ?></textarea><br /> <input type="submit" name="btnSubmit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
mladja04 Posted December 6, 2010 Author Share Posted December 6, 2010 Vauuu, this is great and work, thank you very, very, very much. I guess that I can to add new field in html in which I can to enter name of file and script to create file with that name, not I to create it before code execute. Do it is possible that script create file and to give name - take first words from text field (or first row) and after that to put converted words? Thank you very very much, Mladen Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted December 6, 2010 Share Posted December 6, 2010 Sure, just make up your mind as to what you want to use. Quote Link to comment 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.