Here is a bit of code i have..


<?php
    if (!($fp = fopen("file.txt", "r")))  
exit("Unable to open the input file."); while (!feof($fp))
{
$buffer = fgets($fp, 1024);
print "$buffer<br>\n";
} fclose($fp); ?>

and my markup language looks something like :

------------file.txt-----------------------------
<course> CSCI 1101 </course>

<weight> 30% </weight>



<question> Question 1

	<answer> Answer 1 </answer>
	<answer> Answer 1 </answer>
	<answer> Answer 1 </answer>
	<answer> Answer 1 </answer>

</question>

Im trying to parse these tags into real HTML but teh buffer above doesnt hold any of the tags, just the content between them like "Answer 1" or "30%".

Am I doing this the wrong way, is there an easier way or im i forgetting something.

Thanks a lot..

    change
    print "$buffer<br>\n";
    to
    print htmlspecialchars($buffer) . "<br>\n";

      thanks a lot!

      Now I was look through the string functions on php.net and im trying to figure out away to split the string into tokens but have the tokens split into the tags..

      <thetitile> some title </thetitle>

      would be 3 tokens:

      1.<thetitle>
      2. some content
      3. </thetitle>

      that way I could parse them into HTML...but I would prefer not to have "some content" as 2 tokens but have it as one..is there a function to tokenize these tags or am I going to have to write my own? thx

        With a bit of tweaking to your format (adding a document-level tag), you might be able to use any of the various xml-processing functions that PHP provides. Other than that, using preg_split() could be used to break the string into an array of strings (basically, immediately before '<' and immediately after '>').

          EDIT: i was looking at the code and could it be because in the string there is a '/' because it gets in the if(<thetitle>) but not the if(</thetitle>) ? maybe

          Is anyone familiar with the string tokenizer method.

          I started doing my own approach..heres what i have:

          
          <?php
          
          $answer_check = 0;
          $title_check = 0;
          
          //opening the file
          if (!($fp = fopen("file.txt", "r")))  
              exit("Unable to open the input file.");
             $i=0;
              while (!feof($fp))  
          {
          $buffer = fgets($fp, 1024); $buffarray[$i] = $buffer; $i++; } fclose($fp); //doinf the parsing echo("Parsing!..."); for($j=0; $j < count($buffarray); $j++)// here we visit each string { $tok = strtok($buffarray[$j]," \n\t");//now through each string, current line while ($tok) { if($tok == "<thetitle>") { $title_check = 1; //echo("check is 1"); } if($title_check == 1) { echo("<h1> $tok </h1>"); echo("printing element"); } if($tok == "</thetitle>") { $title_check++; //echo("check is 0 again"); } $tok = strtok(" \n\t"); } } ?>

          My problem is that $title_check gets to = the value '1' but never gets set back to zero so it prints my whole markup language from <thetitle> on...

          I just want to parse what is inside the <thetitle> tag to be html

          am i using the tokenizer properly?

          thanks

            Write a Reply...