Jump to content

Help with Parsing File


psi-phy

Recommended Posts

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:
[code]
$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
[/code]

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:
[code]
<tr>
    <td class="normal">$alphanum</td>
    <td class="normal"><strong>$option</strong> (<em>$paras</em>)</td>
</tr>
[/code]

I know this is wackey and (for me) seemily horrendously complicated, however any help would be VERY appreciated! :)
Link to comment
Share on other sites

Hey, I have a working solution:

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

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

The variable $allmatches will now be like this:
[code]
Array
(
    [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!
        )

)
[/code]

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!
Link to comment
Share on other sites

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:
[code=php:0]
$values = explode(' ', $line, 3);
[/code]

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


:edit:
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
Link to comment
Share on other sites

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:

[code]
<tr>
  <td>Question A</td>
</tr>
<tr>
  <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
</tr>
<tr>
  <td>Question B</td>
</tr>
<tr>
  <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
</tr>
[/code]

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:

[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>";
exit;
}
}
}
?>


//  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>";
}
?>
[/code]

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?
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.