iPixel Posted August 15, 2012 Share Posted August 15, 2012 I need some help. I'm a little lost with something. I'm trying to create a Tab Delimited file from a word file for a Blackboard upload form. Basically it allows you to load a test via tab delimited instead of creating the form by hand each time. On my form post page, i have a <textarea> which basically houses some questions which are posted from a word file. For example... 1. This is question one, below you can answer blah blah? 2. This is another question blah blah blah? 3. This is a multiple choice question answer A *answer B answer C answer D 4. This is a true & false question *True False ======================== My Questions. 1. How can i create a new file? I looked up fputcsv on php.net but they always show examples of already existing csv files. 2. My idea was to str_replace all the \n's with \t's. But I figure that will mess with \n\n between questions where i need to retain a \n. So how do i convert the above info to tab delimited. I'll probably have more questions later, regarding the *'s but this should get me going. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/ Share on other sites More sharing options...
ManiacDan Posted August 15, 2012 Share Posted August 15, 2012 1. How can i create a new file? I looked up fputcsv on php.net but they always show examples of already existing csv files. fputcsv deals with a pre-existing resource handle, not a pre-existing file. Using fopen you can create a new file. 2. My idea was to str_replace all the \n's with \t's. But I figure that will mess with \n\n between questions where i need to retain a \n. So how do i convert the above info to tab delimited. The correct answer is "use a database." If you can't for whatever reason, you can encode the questions and answers so they're sure not to contain quotes or whitespace. base64_encode every field and you won't have a problem. Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369663 Share on other sites More sharing options...
iPixel Posted August 15, 2012 Author Share Posted August 15, 2012 1. Gotcha. 2. I cannot use a database. People will be pasting word file text into it whether i like it or not. -- I get why i would use base64_encode, but how will that help me with tabbing and avoid loosing \n\n's Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369665 Share on other sites More sharing options...
xyph Posted August 15, 2012 Share Posted August 15, 2012 Did you want each question delimited? Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369671 Share on other sites More sharing options...
iPixel Posted August 15, 2012 Author Share Posted August 15, 2012 Yes, unfortunately i would have to turn the questions above into a delimited file structured like so... http://library.blackboard.com/ref/e0bedf24-6f8f-48b8-98ec-8ee774e4efde/upload_questions.htm SO a question like this.... 1. This is a question, with some answers below? answerA *answerB answerC answerD Needs to get formated like so.... MC TAB This is a question, with some answers below? TAB answerA TAB incorrect TAB answerB TAB correct TAB answerC TAB incorrect TAB answerD TAB incorrect Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369673 Share on other sites More sharing options...
ManiacDan Posted August 15, 2012 Share Posted August 15, 2012 1) You can use a database just like you'd use a text file, using a database gets rid of your delimiting problem entirely, just insert it into the database. Whether or not the paste into a textarea is irrelevant, you can insert anything you want into a database. 2) Encoding something using base64 (or whatever) will also avoid your encoding problem. You said you were having issue with \n\n and \t and how they'd interact with the csv format. Just keep them in whatever format you want, and encode them. the encoded data will be compatible with csv format, and the decoded data would still have newlines and tabs however you'd like them. Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369677 Share on other sites More sharing options...
iPixel Posted August 15, 2012 Author Share Posted August 15, 2012 The formatting part is where my issue lies. Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369681 Share on other sites More sharing options...
xyph Posted August 15, 2012 Share Posted August 15, 2012 IS this what you wanted? <?php $raw_question = '1. This is a question, with some answers below? answerA *answerB answerC answerD'; // Split the question up, line-by-line $answers = explode("\n", $raw_question); // Shift the question out of the array $question = array_shift($answers); $final_string = $question; // Loop through answers foreach( $answers as $answer ) { // Add tab to final string $final_string .= "\t"; // Check for correct answer - first character is * if( $answer[0] == '*' ) { // Add everything after the first character in answer to final string $final_string .= substr($answer,1); $final_string .= "\tcorrect"; } else { $final_string .= "$answer\tincorrect"; } } echo $final_string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369687 Share on other sites More sharing options...
ManiacDan Posted August 15, 2012 Share Posted August 15, 2012 What is the actual problem then? What is "the formatting part"? You said: 2. My idea was to str_replace all the \n's with \t's. But I figure that will mess with \n\n between questions where i need to retain a \n. So how do i convert the above info to tab delimited. Did this have nothing to do with CSV output? Are you just looking to replace a single \n with \t as long as there aren't two \n\n? If that's the case: $str = str_replace("\n\n", "__DOUBLE_NEWLINE__", $str); $str = str_replace("\n", "\t", $str); $str = str_replace("__DOUBLE_NEWLINE__", "\n\n", $str); Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369709 Share on other sites More sharing options...
Christian F. Posted August 15, 2012 Share Posted August 15, 2012 I too think this is a really bad idea, and that you should indeed use a database as previously suggested. That said, I think you'll find that [ci]addcslashes ()[/ic] will help you out: $str = addcslashes ($str, "\n\t\r"); Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369732 Share on other sites More sharing options...
iPixel Posted August 16, 2012 Author Share Posted August 16, 2012 What xyph did, is mostly how i envisioned it. It's about 98% solid... this gives me a great start. Thanks for that. @ManiacDan: I hope i'm not frustrating you, I just don't see a need to use a database. I'll still have to parse the questions either before data entry or after data entry. This seems like an extra step to me. To see the formatting i need done, please go to File Structure here... http://library.blackboard.com/ref/e0bedf24-6f8f-48b8-98ec-8ee774e4efde/upload_questions.htm Xyphs script is working well, i just need to work it out so it figures out the type of question it is. "MC" "MA" "ESS" etc... Thanks very much for all the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369859 Share on other sites More sharing options...
ManiacDan Posted August 16, 2012 Share Posted August 16, 2012 I'm not really frustrated, I just honestly have no idea what you're even doing anymore. You have two scripts now which do two things you may or may not need. I don't know what you're using which requires tab delimited strings rather than a normal, sane data structure like an array, but good luck with it. If this is still not what you needed, go back and state the problem more concisely. What do you have? What do you need? Why do you need it that way? Quote Link to comment https://forums.phpfreaks.com/topic/267131-parsing-text-creating-tab-delimited-file/#findComment-1369869 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.