Displaying text content on a PHP image, using text from another site - php

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?

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.

Related

Is that possible to extract doc/docx file page wise in php?

I have a web form where user needs to upload a document file (doc or docx), in that file there is some formatted data on multiple each page. so I want to extract that data but I also need to know page number as well. That means I want content of each page separately, is it possible?
Thanks
Its not in raw format, so you want this maybe to interpret the code of docx.
https://github.com/PHPOffice/PHPWord Check this.

Using JQuery or PHP to pull latest image?

I was trying to figure out a way to have JQuery (if possible) to display the latest image from a directory, regardless of its file name. Basically, I just want a simple tag to be updated to show the image. I realize this might not be possible with Jquery, so my fallback would be PHP.
I've been trying to research how to do this, but honestly do not know where to start. Any tips to get going would be greatly appreciated,
If you want to do it with JQuery, you can! You only need to add a few lines of logic to your app.
For example, when you add images to that folder, also write a text file with the name of the image just added. Then this text file is going to always have the filename of the last image uploaded. Finally, using JavaScript(with JQuery or anything else) you do one request to the text file and then you do the second request to obtain the image with that filename.
That is just a very basic example to try to share the idea of how to do it, then depending your particular case, you can add or change a lot of details to make this works for your case.
BTW, to do it with PHP, you are going to need to execute a system command, for example 'ls -ltrh' and parse the output to obtain the last filename.
You can use PHP script to get latest modified file in a specific directory.
Code can be found here: Get last modified file in a directory
You then can format that into JSON which can be passed through a jQuery AJAX call http://api.jquery.com/jQuery.ajax/
With those 2 techniques, you can manipulate the data to display the image.

How to make php pages load faster when fetching bulk of information?

Basically I am building a website that does web scraping and fetches particular web pages from around 8 different websites to extract price. I am using file_get_html() function of PHP Simple HTML DOM Parser extensively to fetch the page source into a string variable and extract the price information out of that.
Now the main problem is the page which shows the price information from all different sites is taking very long time to load.
So my question is
How to make the page load faster. &
How to load the page in steps so that those information which has been fetched loads and other information will load subsequently like google image search.
Don't fetch the data on page load, but do it in a background job (cronjob?) and save it in the database.
So you will only have to retrieve the data from the database. Additionally you could add a text with a timestamp when the data has been retrieved and / or give the user the ability to manually update (get) the data.
Well, first of you can use cURL instead of file_get_html(), It's easy and very configurable + it's faster than using the simple html dom function. Obviously you will have to convert the string into a dom object using the simple html dom function str_get_html() after that.

Editable grid supporting XML data storage with row editing/sorting/reordering

Is there an editable grid made for xml with a sensible api for editing, sorting, reordering rows of text, numeric, hyperlink data? This HTML page requires a grid to show and edit data stored in an XML file. I'd like to have a grid that allows row selection and deletion. I've looked at
DHTMLXgrid: There is no documentation on how to save data to XML files. If you have information about to do that, I might have another look at it.
Flexgrid: Doesn't do editing yet.
DrasticTools: No XML example, and interface non-standard.
slickgrid: A lot of the examples supplied are broken. Can't show that the features are actually there. Seems like only the initiated can use this grid.
jqgrid: Prefer to stay away from that one.
Are there alternatives? Either javascript or PHP would be OK.
Dhtmlxgrid does the job wery well. Found information on using the api to get a text string with the current grid content. From this I was able to save to XML. More information is given here Dhtmlxgrid: Getting an xml string of grid data

Convert HTML & CSS to DOC(X)?

Is there some utility that could be called via command line to produce a doc(x) file? The source file would be HTML and CSS.
I am trying to generate Word documents on the fly with PHP. I am only aware of phpdocx library, which is very low level and not much use for me (I already have one poor implementation of Word document generation).
What I need from a document:
TOC
Images
Footers/Headers (they could be manually made on each HTML page)
Table
Lists
Page break (able to decide what goes to which page, eg one HTML file per page, join multiple HTML files to produce the entire document.)
Paragraphs
Basic bold/etc styles
I didn't find PHPDOCX very useful either. An alternative could be PHPWord, i think it covers what you need. According the website it can do these things:
Insert and format document sections
Insert and format Text elements
Insert Text breaks
Insert Page breaks
Insert and format Images and binary OLE-Objects
Insert and format watermarks (new)
Insert Header / Footer
Insert and format Tables
Insert native Titles and Table-of-contents
Insert and format List elements
Insert and format hyperlinks
Very simple template system (new)
In your case that isn't enough, but there is a plugin available to convert (basic) HTML to Docx and it works very good in my opinion. http://htmltodocx.codeplex.com/
I am using this for a year or two now and am happy with it. Altough i have to add that the HTML can't be to complex.
The way I usually do these is to have a word document template file with the parts I want to replace using keywords (usually something like "{FIRSTNAME}").
This allows you to read the file via PHP then simply do str_replace on all the parts you want to replace, then write that to another file.
Dynamic tables using this method are a bit more tricky, as you need a sub template for a row, which you can then include inside the main template as many times as required.
I'm not sure if this is the best solution, it's always seemed very fiddly to me and every time I'm asked to do this I get frustrated with it, but I guess it works. So if anyone knows a better solution I'd love to hear it too!

Categories