This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
angel14995


Member


Joined: 13 Oct 2007
Posts: 181

Posted: 21 Apr 2008 08:24:20 pm    Post subject:

Hello all-

I am working on a way to retrieve a specific number from a web page. The problem is I need this script to find the number, and give it to me as a number so I can manipulate it with some math. The problem is that I am going to be using this code on multiple web pages (not running it inside them, just 1 page runs it), so it needs to be able to find the number I am looking for with just a little help. Here is what I have so far:


Code:
//Find the item in the RuneScape Grand Exchange Database
$url = 'http://http://itemdb-rs.runescape.com/viewitem.ws?obj=' . $itemname;

//Gets the contents of the webpage and stores it to a variable
$rs_string = file_get_contents($url);

//Searches the string for <b>Market price:</b> 2 </span>, but for me, the getting for the exact part is hard. The 2 is a changing number
preg_match_all(' <--This is my main problem, what should go here?--> ', $rs_string, $info);      

//Converts the $info into a string, strips the tags, then takes the last little bit as the output
$cost = $info[1][0]
$cost = strip_tags($cost)
$cost = substr($cost, 15)
echo $cost


Anyone that can help, I will much appreciate it.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 21 Apr 2008 08:42:44 pm    Post subject:

Try using the regular expression:

Code:
<b>Market price:</b> \b\d+\b </span>
Back to top
angel14995


Member


Joined: 13 Oct 2007
Posts: 181

Posted: 21 Apr 2008 08:44:54 pm    Post subject:

Well can you explain it to me so I'm not totally confounded. The whole \b\d+\b gets me no clue what is happening. I know about regular expressions somewhat, but pity me and explain a little please.

Last edited by Guest on 02 Aug 2010 02:30:23 am; edited 1 time in total
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 21 Apr 2008 09:36:01 pm    Post subject:

\b\d+\b represents any integer. The \b marks a word boundary (I'm not sure if it's necessary, so if this doesn't work, try removing them). The \d represents any digit from 0-9. The + after \d means that there can be any number of digits in a row, forming one integer.
Back to top
alexrudd
pm me if you read this


Bandwidth Hog


Joined: 06 Oct 2004
Posts: 2335

Posted: 21 Apr 2008 10:04:26 pm    Post subject:

Wow, I haven't been on RuneScape in a while. That new exchange looks pretty convenient; what are you planning on doing with the data you parse?


/me looks at price of Santa hat, which I bought for ~250k years ago. Very Happy
Back to top
angel14995


Member


Joined: 13 Oct 2007
Posts: 181

Posted: 22 Apr 2008 05:53:33 am    Post subject:

@Magicdanw: Thanks, that helps he a lot. I think I understand it now. I tried what you siad, and when I open it in an HTML document, I have the webpage dispaly as "Market price: \b\d+\b ', $rs_string, $info); $cost = $info[1][0]; $cost = strip_tags($cost); echo '$cost'; ?>". The code for the page is:

Code:
<html>
<header></header>
<body>
<?php
$url = 'http://http://itemdb-rs.runescape.com/viewitem.ws?obj=2171';
$rs_string = file_get_contents($url);
preg_match_all('<b>Market price:</b> \b\d+\b </span>', $rs_string, $info);
$cost = $info[1][0];
$cost = strip_tags($cost);
echo '$cost';
?>
</body>
</html>


Can you/anyone help me now? Like I said, I'm still new to PHP, and finding errors in any code has never been my strong point.


@AlexRudd: I'm planning to take the information and make an external GE Database of sorts. With that data, I can then get extremely updated and accurate numbers for things like making money. For example, it would be like the program finds the prices of coal, iron, and steel bars, figures out the net profit/loss per bar, investment percentage, and things like that.
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 22 Apr 2008 08:37:21 am    Post subject:

If you're looking for a way to make money off the GE, I recommend purchasing bronze arrows at 5gp each and then selling them for 6gp. You can buy like 200k arrows for 1mill and then sell them for 1.2mill. It may take about 3-4 days for the transaction, but you should be able to do it over and over again. And you don't have to ever risk losing money. Because worst comes to worst, you can sell the bronze arrows for 5gp.

If you think I still play runescape, I don't. I have a friend that uses this technique. I used to play rs about 4-5 years ago, but I forgot my password for my character and the recovery questions. I didn't feel like leveling another character up to lvl 80 so I quit. If you want to guess the password for 900310 you are more than welcome to try.

I'm sorry that I can't help you with the php. I am a noob at php coding as well, and once they started doing that \b\d+\b thing I got lost. I wish you luck.
Back to top
Demon


Advanced Member


Joined: 17 Jun 2006
Posts: 369

Posted: 22 Apr 2008 07:20:52 pm    Post subject:


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
  <title>GE Database</title>
   </head>
   <body>
   <?php
  $url = 'http://itemdb-rs.runescape.com/viewitem.ws?obj=2171';
  $rs_string = file_get_contents($url);
  preg_match_all("|<b>Market price:</b> \d+|", $rs_string, $info);
  $cost = strip_tags($info[0][0]);
  echo $cost;
   ?>
   </body>
</html>


...Or...


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
  <title>GE Database</title>
   </head>
   <body>
   <?php
  $url = 'http://itemdb-rs.runescape.com/viewitem.ws?obj=2171';
  $rs_string = strip_tags(file_get_contents($url));
  preg_match_all("|Market price: \d+|", $rs_string, $info);
  $cost = $info[0][0];
  echo $cost;
   ?>
   </body>
</html>


...in which stripping the tags out from the start may make it easier for you to parse.


Last edited by Guest on 22 Apr 2008 07:33:26 pm; edited 1 time in total
Back to top
cjgone
Aw3s0m3


Active Member


Joined: 24 May 2006
Posts: 693

Posted: 23 Apr 2008 01:03:37 am    Post subject:

offtopic:

Runescape ftw.. Finally got 2 99 skills in that game. :D

Whats your user, add me :]
Back to top
David
The XORcist!


Advanced Member


Joined: 20 May 2003
Posts: 268

Posted: 23 Apr 2008 03:31:50 am    Post subject:

Hey guys, as far as I can see, there's only one single item that needs to matched.
In that case, preg_match() may suit better than preg_match_all().

Code:
 $regex = '|<b>Market price:</b>\s+(\d+)\s*</span>|';
 if (preg_match($regex, $rs_string, $result)) {
   $cost = $result[1];
 } else {
   echo "Cannot parse price tag!";
 }

The gibberish in the regex </b>\s+(\d+)\s* means the following:
A closing </b> tag followed by:
* one or more space characters, then
* one or more digits, then
* zero or more spaces.


Parentheses in a regex denotes a subpattern.
The regex would still match if you write \s+\d+\s*, but the parentheses placed around \d+ allows us to extract the sequence of digits neatly. That sequence will be found at index 1 in $results, since it is the first subpattern in the expression. Btw, index 0 in $result holds the string which was matched by the entire pattern.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement