Jump to content

Use PHP to add html tags to a text file


chaosxkitten

Recommended Posts

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

?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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 */
}
?>

Link to comment
Share on other sites

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.