1

I have a dynamic image coded in PHP, which is supposed to serve as a display for statistics, using an external site. Each statistic will be displayed using the following general code:

    imagettftext($template,10,0,5,35,$black,$font,$statVar1);

...where $statVar1 is the name of the first statistic that will be shown (I have a total of 99 to display).

The only way I know how to do this is to use PHP with JSON, to save the data to a file, and then load them, whenever the image is displayed, but I don't need that sort of data saving in this case, so all I am looking for is just to display whatever the content is on the source page at the same time that the image was loaded.

My JSON solution involved getElementsByTagName, and I would select out the HTML tags and select the right one with item(), but is there a pure PHP solution I could use instead? For instance, if I wanted to visit the DC Vault site, and set my $statVar1 to the Overall Position value of the first team (ie, display '1' on my stat image), what would I do?

Hiigaran
  • 829
  • 10
  • 28

1 Answers1

0

If I understand correctly, you want to extract data from another page/site in your PHP code.

The simplest way is to use something like the file_get_contents() with an URL as parameter instead of a regular file path. Learn about PHP URL wrappers. https://www.php.net/manual/en/wrappers.php

You can than parse returned data to extract what you need.

If the respective site does not provide any export in structured format like XML or JSON, you will need to parse HTML document.

Regular expressions would be a way to go. See the PHP function preg_match.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • When parsing that html, DO NOT USE REGEX. And if you do, keep in mind that question has been answered far too many times here. Use DOMDocument. – 000 Apr 13 '13 at 21:38
  • Ahh, I'm going to need a little more help on that. I'm not sure how preg_match helps. php.net didn't help me, either. I'm an absolute beginner with PHP (my experience with it has been copying, pasting, and hoping it works). Looking at related questions, I found a link to something that shows promise, but I'm not sure how to properly use it to find the fields I'm looking for: http://simplehtmldom.sourceforge.net/ – Hiigaran Apr 13 '13 at 22:21
  • Joe will help you, for sure :) I've tried, but found it insane task to parse that horrible DC Vault HTML using DOM :( – Martin Prikryl Apr 14 '13 at 07:54