chaosxkitten Posted February 3, 2011 Share Posted February 3, 2011 Yes, it's a homework assignment. No, I'm not trying to cheat, so pointing me somewhere is better than just feeding me code, like, "look up how to blah blah" or tell me what I've got wrong? I'm trying to parse a text file of the constitution, add header and <p> tags depending on the first word of the paragraph, then print to an html page. I know I'm supposed to create a function that will return a paragraph, and another function that will return the first word of a paragraph. This is my code, and it completely does not work (obviously I've not included the external html tags) <?php // Opens the constitution text file. $const = fopen("constitution.txt","r"); //Returns an entire paragraph function getParagraph($myfile) { while (!feof($myfile)){ $line = file_get_contents($myfile); $paragraph = explode("\n",$line); } return $paragraph; } //Returns the first word of a paragraph function getFirstWord($myfile) { $pg = getParagraph($myfile); $word = explode(" ",$pg); Return $word[0]; } // Runs through some if statements to determine the tags to use if (getFirstWord($const) === "Article"){ echo "<h2> getParagraph($const)</h2>"; } elseif (getFirstWord($const) === "Section"){ echo "<h3> getParagraph($const)</h3>"; } elseif (getFirstWord($const) === "Amendment"){ echo "<h3> getParagraph($const)</h3>"; } elseif (getFirstWord($const) === "We the People"){ echo "<em> getParagraph($const) </em>"; } else{ echo "<p> getParagraph($const) </p>"; } fclose($const); ?> Quote Link to comment Share on other sites More sharing options...
chaosxkitten Posted February 3, 2011 Author Share Posted February 3, 2011 I'm using emacs if that matters. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 3, 2011 Share Posted February 3, 2011 those === should probably be == Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 3, 2011 Share Posted February 3, 2011 Without knowing the format of the input file it is difficult to provide any guidance. What data is in the data file that you can use to determine when a paragraph ends and another begins. If a hard line break is used to determine paragraphs you might want to look at fgets(). There are some problems in your code above where you are mixing things that shouldn't be. You have fopen() to open the file for reading, but then you use file_get_contents() that doesn't need fopen() to be called before it. So, if you do use fgets() then you do want to use fopen(). Another option is to use file() which will read the file into an array with each line a separate element in the array. Secondly, the function getParagraph() is written so it tries to read ALL the paragraphs from the file, not just the next one. Also, the function getFirstWord() is trying to get the word from the file. You should be passing the paragraph to the function. The process you seem to be trying to achieve - based on the names of the functions seems appropriate. But the functions are not doing what they are named. So, fix the function to getParagraph() either returns the next paragraph from the file or returns false if the end of the file is reached. Then change getFirstWord to parse out the first word from the paragraph. Then in your main function you can create a while loop and perform whatever operations you need to. Here is another tip. Only work on ONE process at a time. I would suggest just working to extract and echo each paragraph to the page. Then move on to doing something more with that data. There's no need trying to parse out the first word when you aren't even getting the paragraphs. So, start with a process such as this: while($paragraph = getParagraph($const)) { echo "Paragraph: " . $paragraph . "<br /><br />\n"; } Quote Link to comment Share on other sites More sharing options...
chaosxkitten Posted February 3, 2011 Author Share Posted February 3, 2011 The file is broken up exactly how it needs to be displayed. I am exploding with line breaks. This response helps quite a bit and I'll see what I can do. I get frustrated and just try to move on. Quote Link to comment Share on other sites More sharing options...
chaosxkitten Posted February 5, 2011 Author Share Posted February 5, 2011 bump... still having trouble getting the first function to return the next paragraph Quote Link to comment Share on other sites More sharing options...
sunfighter Posted February 5, 2011 Share Posted February 5, 2011 I'll give you a start, OK? A file resides on your hard drive, when you open one you read the entire file into memory and get a POINTER to the start of the file in memory. SO, $const = fopen("constitution.txt","r"); puts constitution.txt into mem and $const is a pointer to it. When you read the file into a string with $cfile = file_get_contents($myfile) the file_get_contents needs the file name not a pointer. You are sending the pointer to the function and not the file name. Also you are going to need the pointer.... So I would suggest you use fopen instead. You can limit the amount read and can find the first word with ease....hint - look for the first space.. Good Luck need more help just ask Quote Link to comment Share on other sites More sharing options...
chaosxkitten Posted February 6, 2011 Author Share Posted February 6, 2011 this is what I have now, still doesn't print anything but "array". A lot is in comments because I'm just trying to get back one paragraph. <?php // Two user created functions: //Returns an entire paragraph function getParagraph($myfile) { while (!feof($myfile)){ $data =fgets($myfile); $paragraph = explode("\r\n",$data); // preg_replace('/r?\n/',' ',$paragraph); } return $paragraph; } //Returns the first word of a paragraph function getFirstWord($p) { $word = explode(" ",$p); return $word['0']; } //Those functions in use: // Opens the constitution text file. $const = fopen("constitution.txt","r"); while ($paragraph = getParagraph($const)){ // print_r($paragraph); echo "Paragraph: ".$paragraph."<br />\n"; } // Runs through some if statements to determine the tags to use //$n = 0; //do { //while ($var = getParagraph($const)){ //if (getFirstWord($var['$n']) == "Article"){ //echo "<h2>$var['$n']</h2>"; //} //elseif (getFirstWord($const) == "Section"){ //echo "<h3>$var['$n']</h3>"; //} //elseif (getFirstWord($const) == "Amendment"){ //echo "<h3>$var['$n']</h3>"; //} //elseif (getFirstWord($const) == "We"){ //echo "<em>$var['$n']</em>"; //} //else{ //echo "<p>$var['$n']($const) </p>"; //} // $n += 1; //} //} while (count($var) >= $n) fclose($const); ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 6, 2011 Share Posted February 6, 2011 1. please use the php tags when posting code. 2. try this... <?php /* presuming only paragraphs end with newline character "\n" */ /* this will put all the paragraphs into an array */ $paragraphs = file("constitution.txt"); /* remove empty elements */ $num_paragraphs = count($paragraphs); for($i = 0; $i < $num_paragraphs; $i ++) { $paragraphs[$i] = trim($paragraphs[$i]); } $empty_elements = array_keys($paragraphs,""); foreach ($empty_elements as $e) unset($paragraphs[$e]); /* test it by printing the array */ echo "<PRE>"; print_r($paragraphs); echo "</pre>"; /* now you can take each element of the array and work on them */ $num_paragraphs = count($paragraphs); for($i = 0; $i < $num_paragraphs; $i ++) { /* do whatever to each element/paragraph */ } ?> Quote Link to comment Share on other sites More sharing options...
chaosxkitten Posted February 6, 2011 Author Share Posted February 6, 2011 Thank you, that's pretty much what I did... sorryididntknowaboutthis 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.