Jump to content


Help with Parsing File

  • Please log in to reply
3 replies to this topic

#1 psi-phy

  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 13 July 2006 - 03:06 PM

Greetings all!

  I have a file that is plain TXT but I need to add some HTML/PHP tags to it because it's a form that I am converting for a web-based form.  Now I figured out how to fopen a file and explode each line.  That I get.  What I would like to be able to do is separate the LINE now into three parts.

  I have a sneaky suspicion that regex is needed, however I wouldn't have the first clue how to start.

Here is an example of what I need to do:
$line = A. Option (quick explanation in parentheses)

// In that line, it always starts with either a capital letter followed by a period, or a number followed by a period.
// Secondly there would be the main Option
// Thirdly there is the explanation which is ALWAYS in the ()'s

So I need to explode $line, but how do I break it up just the way I need?  The outcome is so I have three variables:  $alphanum, $option, $paras

I want to put the three variables into a foreach loop (taking each exploded line) so it would look like this:
     <td class="normal">$alphanum</td>
     <td class="normal"><strong>$option</strong> (<em>$paras</em>)</td>

I know this is wackey and (for me) seemily horrendously complicated, however any help would be VERY appreciated! :)

#2 ShogunWarrior

  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 13 July 2006 - 03:23 PM

Hey, I have a working solution:

Here's the test input: (list.txt)
$line = A. Option (quick explanation in parentheses)
$line2 = 22. CoolWickedOption (Please enter your paranthese!)

And the PhP code:
$file = file('list.txt');
$size = count($file);
$allmatches = array();
        $line = $file[$i];
        $preg = preg_match('/\$+([^\s]+)[ ]*?={1}[ ]*(.*?)\.{1}[ ]*?([^\s]+)[ ]*?\((.*?)\)/i',$line,$matches);
                $allmatches[] = $matches;

The variable $allmatches will now be like this:
    [0] => Array
            [0] => $line = A. Option (quick explanation in parentheses)
            [1] => line
            [2] => A
            [3] => Option
            [4] => quick explanation in parentheses

    [1] => Array
            [0] => $line2 = 22. CoolWickedOption (Please enter your paranthese!)
            [1] => line2
            [2] => 22
            [3] => CoolWickedOption
            [4] => Please enter your paranthese!


So you can cycle through the array and the corresponding values are:
[0 ] => The entire matched string, not really needed.
[1]=> The variable name at the start of the line.
[2]=> The letter/number before the period.
[3]=> The option name.
[4]=> The parenthese part.

Hope it helps!
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 Koobi

  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 13 July 2006 - 03:29 PM

i'm assuming your options have no spaces in them since you haven't stated otherwise. if it does, you would probably have to use regular expressions.

but for now, how about:
$values = explode(' ', $line, 3);

the above code will explode by a space and only three times. once 3 explodes are executed, it will stop.

ShogunWarrior just posted while I was posting.
do we have to parse $line itself as well? or is $line already parsed at this point?
if you have to parse the entire line and if there's a space on either side of the equals sign, use 5 instead of 3 and disregard the first two indices of the array $values

#4 psi-phy

  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 13 July 2006 - 08:24 PM

Thank you both, however I had to go another route because I really don't get regex and Shogun's approach didn't work.

(I can only guess the problem was that the input text file was not accurate with the spacing and such and didn't conform to the Expression that Shogun gave me to use.  :) )

To resolve the problem, I tried some search-and-replace and added a <!--break--> where I needed one.  So I just exploded the file with that.

I, however, have run into another problem.  Each line of that file was an answer to a question on a form.  It eventually came down to this:

  <td>Question A</td>
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 1 for Question A
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 2 for Question A
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 3 for Question A
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 4 for Question A
  <td>Question B</td>
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 1 for Question B
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 2 for Question B
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 3 for Question B
  <td> (radio button) $alphanum $option $parenthesis</td>  //Answer 4 for Question B

So all this is in a form, there are A through U amount of questions.  On the receiving end of the form submit, I wanted to have a quick screen to confirm the answers.  This is that code:

//  This array is a list of all the question letters from the form up there
	$questions = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U");

//  This was my lame hack to check that ALL radio buttons actually HAD a value, if not it errored out.
	for ($i=0; $i < count($_POST); $i++) {
		for ($i=0; $i < count($questions); $i++) {
			if (!array_key_exists($questions[$i], $_POST)) {
				echo "<font class=\"largebold\">ERROR: </font><br />";
				echo "<font class=\"warning\">Question " . $questions[$i] ." was not answered.  "._CLICKBACK."</font>";

//  This is the table of confirmation for the data.
<table align="center" border="1" cellspacing="0" cellpadding="0" width="20%">
	for ($i=0; $i < count($questions); $i++) {
		echo "<tr>";
		echo "<td align=center class=bold width=50%>";
		echo $questions[$i];
		echo "</td>";
		echo "<td align=center class=normal width=50%>";
		echo $_POST[$i];
		echo "</td>";
		echo "</tr>";

Now up there, you may have noticed the " $_POST[$i] " in the table.  I assume you could traverse a global variable like that as if you could traverse any other array.  So then I tried to "trick" it, by adding a "  $answers = $_POST;  " before the table, which did nothing.

So how do I traverse the $_POST variable so it gives me the values of each $_POST key?

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users