Page 1 of 1

Parsing from HTML File

Posted: Tue Jul 22, 2008 6:14 pm
by JB4
Hey there! I was wondering if someone could help me with parsing a number from an HTML File.

basically what it says in the source of the file is:

Code: Select all

<TD><b>Price</b>:&nbsp;$0.50&nbsp;&nbsp;&nbsp;
All I want is the 0.50

So How do I have it parse that number?

Thanks.

Re: Parsing from HTML File

Posted: Tue Jul 22, 2008 7:00 pm
by Dynamis
Well you didn't give me much info, but based on what you said, you need to cut off everything before the 0.50 and substring it till the next & symbol. Give the link to the page you are trying to do this on for a specific example if you do not understand this.

Re: Parsing from HTML File

Posted: Tue Jul 22, 2008 7:11 pm
by WebbieDave

Code: Select all

$price = array();
preg_match('/\$(\d+)?\.\d+/', "<TD><b>Price</b>:&nbsp;$0.50&nbsp;&nbsp;&nbsp;", $price);
Then chop the $
Anything more and you'll probably need an HTML parser which you can easily google for.

Re: Parsing from HTML File

Posted: Tue Jul 22, 2008 7:32 pm
by Dynamis
WebbieDave wrote:

Code: Select all

$price = array();
preg_match('/\$(\d+)?\.\d+/', "<TD><b>Price</b>:&nbsp;$0.50&nbsp;&nbsp;&nbsp;", $price);
Then chop the $
Anything more and you'll probably need an HTML parser which you can easily google for.
Its actually only like one line of code to get just the $0.50. I just need to see the rest of the page to give you the code for it. You have to check for specific things in the page, cut to that point, then find first occurrence of $, and substring from there to first & symbol. Post the page where you want to do this from if you want the code.

Re: Parsing from HTML File

Posted: Tue Jul 22, 2008 8:54 pm
by manixrock
You probably need to extract that value from a big HTML source file, so a more specific regexp should be used:

Code: Select all

preg_match('/Price.{0,10}?:.{0,20}?(\d+(\.\d+)?)/i', "<TD><b>Price</b>:&nbsp;$0.50&nbsp;&nbsp;&nbsp;", $groups);
$price = floatval($groups[1]);

Re: Parsing from HTML File

Posted: Tue Jul 22, 2008 10:06 pm
by JB4
Whoops my 2nd post didn't get posted. Sorry about that.

http://www.sagatraders.com/store/CS_Human_Clerics.html

What I need is to take what they select from the drop down list, and add it where the Human_Clerics is. SO basically it needs to make it this:
http://www.sagatraders.com/store/CS_[i](Their selection)[/i].html
and then copy the price from it.

So far I have it setup how I need here:
http://sagasupporters.mygamesonline.org/

just the last page is what i cant get.

so my site starts off like this:

Code: Select all

<form action="welcome.php" method="post">
<input type="radio" name="faction" value="Light" checked="checked" />
<img src="Light.gif" width="50" height="50" /> Light
<br />
<input type="radio" name="faction" value="Machines" />
<img src="Machines.gif" width="50" height="50" /> Machines
<br />
<input type="radio" name="faction" value="Magic" checked="checked" />
<img src="Magic.gif" width="50" height="50" /> Magic
<br />
<input type="radio" name="faction" value="Nature" />
<img src="Nature.gif" width="50" height="50" /> Nature
<br />
<input type="radio" name="faction" value="Neutral" />
<img src="Neutral.gif" width="50" height="50" /> Neutral
<br />
<input type="radio" name="faction" value="War" checked="checked" />
<img src="War.gif" width="50" height="50" /> War
<br />
<input type="submit" />
</form>

It redirects to here:

Code: Select all

You selected the <?php echo $_POST["faction"];?> faction. 
 <img src=<?php echo $_POST["faction"];?>.gif width="50" height="50" />
<META HTTP-EQUIV="Refresh"
      CONTENT="2; URL=<?php echo $_POST["faction"]; ?>.php">
which redirects to here:

Code: Select all

   <?php
$your_variable = "0";
$your_file = "Light Units.txt";
 
 
        $read_your_file = @fopen($your_file, "r") or die ("Couldn't Access $your_file");
 
 
        $contents_your_file = fread($read_your_file, filesize($your_file));
        
 
        $your_array = explode("\n",$contents_your_file);
 
 
        fclose($read_your_file);
 
 
        $num_elmnts_array = count($your_array) ;
 
 
 
 
$your_menu = "<select name=\"Unit\">";        
 
        for($counter = 0; $counter < $num_elmnts_array; ){
         $your_menu .= "<option value=$your_array[$counter]>$your_array[$counter]
</option>";
                $counter++;
                $your_variable++;
        }
 
$your_menu .= "</select>";
?>
 
<form action="results.php" method="post">
<p><b>Pick a Unit</b><br>
<br>
<?php echo "$your_menu";?>
<br>
<br>
 
<input type="submit" />
</form>
this then goes to my last page which is what my original question was for:

Code: Select all

<?php $url = "http://www.sagatraders.com/store/CS_"; 
echo $url;
echo $_POST["Unit"];
echo ".html";
?>
<BR>You searched for <?php echo $_POST["Unit"]; ?>.
 
So I've got it to SAY what I want to read, and parse. But how do I get it past there?
I would really like to get away from the tag team html and php if possible, and go to just mostly php but I don't know how plausible that is, are there php replacements for my html things?
Also any suggestions on making the coding better would be great!

Thanks for your help!

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 1:40 pm
by JB4
Another question, how would I make the drop down list not need the _ in them, so It doesn't look as tacky. I tried it without, but it just got the first word.

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 2:00 pm
by Dynamis
So you got it to print the $0.50 and no longer need help with that?

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 2:08 pm
by JB4
Ok so I got it like this now:

Code: Select all

<html>
<head>
<title>
<?php echo $_POST["Unit"]; ?>
<body>
<?php $URL = "http://www.sagatraders.com/store/CS_" . $_POST["Unit"] . ".html";
?>
<BR>You searched for <?php echo $_POST["Unit"]; ?>.
<BR><BR>Getting price from <?php echo $URL ?>
</body>
</html>
(can be seen HERE by selecting a unit)

What I need to do is get the html page that is linked, and get line number 179 from it. It's always line 179. How would I do that?

and the other things I'd like are:
I would really like to get away from the tag team html and php if possible, and go to just mostly php but I don't know how plausible that is, are there php replacements for my html things?
Also any suggestions on making the coding better would be great!

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 6:16 pm
by JB4
Ok, now it's like this:

Code: Select all

<html>
<head>
<title>
<?php echo $_POST["Unit"]; ?>
<body>
<?php $URL = "http://www.sagatraders.com/store/CS_" . $_POST["Unit"] . ".html";
$lines = file($URL);
$line179 = $lines[179];
preg_match('/Price.{0,10}?:.{0,20}?(\d+(\.\d+)?)/i', $line179, $groups);
$price = floatval($groups[1]);
?>
<BR>You searched for <?php echo $_POST["Unit"]; ?>.
<BR><BR>Getting price from <?php echo $URL; ?>
<BR><BR><?php echo $_POST["Unit"]; ?> costs <?php echo $price ?>
</body>
</html>
here is the outcome:
You searched for Juggernaut.

Getting price from http://www.sagatraders.com/store/CS_Juggernaut.html

Juggernaut costs 0
problem is somewhere in there it doesn't grab the right thing, and the price comes out as "0" Any Ideas?

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 7:43 pm
by JB4
I decided to Upload my current files in case someone didn't understand what I've got so far.

These are up to date, and I tried to add comments on what is going on where I thought you needed it.

Results.php is the file that I can't get to work.

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 7:52 pm
by manixrock
you do not need to separate the source code into lines. In fact it's a really bad ideea. Just run the regexp directly on the page source:

Code: Select all

<?php $URL = "http://www.sagatraders.com/store/CS_" . $_POST["Unit"] . ".html";
$page_source = file_get_contents($URL);
preg_match('/Price.{0,10}?:.{0,20}?(\d+(\.\d+)?)/i', $page_source, $groups);
$price = floatval($groups[1]);
?>

Re: Parsing from HTML File

Posted: Wed Jul 23, 2008 7:55 pm
by JB4
YAY FOR MANIXROCK!!!!!!!

Thank you so much, I've been working on it for 8 hours, and you just came along and made it a lot easier for me. Thank you so much!