Jump to content

Recommended Posts

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!

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.

 

 

 

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

 

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.

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;

?>

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);

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");

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!

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.