How to edit a php/html file using PHP - php

I'm trying to design an interface to change <img> sources throughout my website, so that I don't need to change the sources by hand anytime we want to change what images my site displays.
I looked at DOMDocument already, and while it's useful for accessing the different elements in a file, it wraps the files in it's own HTML tags, which messes up the files (I already tried looking here but those solutions didn't work for me)
Is there a better way to take a file, retrieve element information and attributes (like src and id) from img tags, edit those elements, and save the file?

Just to close this up - I ended up using Querypath to accomplish this task. It works pretty well and was pretty simple to utilize.

Related

css style php as function

While i am still learning css html5 and Jquery, combined with the information from the internet, i came up with a idea to program the css in to different chapters, and call them in to the head section when need it.
from my understanding, via google it is faster to load css styles in the head as a <style></style>.
This way i will only have the css at the page that i need, and it will be better readable for me, becouse i dont use html css styling in dreamweaver anyway.
My css is now so big it is taking tomutch time to find it all.
even with dreamweaver search box.
Does any one have experience with this, or see any problems i might incounter?
Thanx in advance..
The only way to create structure if you're working in a big php project is to save a blank html page, load the existing css on to that page, use the css options/panels in dreamweaver and load the body parts you need in there.
If your using a dynamic css location link, other wise, i think dreamweaver will do just fine in php.
Not sure becouse i never used it that way...
Situation
i try'd to make more sence doing it this way, but i have just put everything back, becouse i got a unknown error, did not investigate it further. And the <style></style> has the highest level of design, meaning all the rules that where set, will be overwritten, things will get complicated fast if you start moving parts aspecialy if have a multi css styling site, so it is not recommended if you already have a big project, do it from the start...
Edit * 27-11-2015
I found the solution, i am using classes to slowely but surely replace the css file...

Advanced ePub reader

I'm trying to build an advanced ePub reader using jQuery and PHP/Zend Framework1.12 (for the epub3.0 format). The reader should contain the following features:
books should be displayed using pages (2 pages at a time)
the user should be able to navigate between pages and chapters using a slider
the user can create highlights and bookmark pages
the reader must be cross-browser (I don't care much about older versions of IE, but it must work on Safari, Mozilla, Chrome)
My idea is to make some kind of PHP parser that will handle the epub content and pass it on to the Javascript code in a more 'friendly' format, but I haven't worked with epubs before and I'm not sure where to start.
Here are a few questions that I have been struggling with:
The first problem I have encountered is how to extract the content from an .ePub archive and render it in a such a way that will allow the paginated view. What PHP library would you recommend for parsing epubs? I have already tested some libraries like BookGluttonEpub (seems quite old) and EPUBParser (difficult to understand since there are no examples and docs). Are there others I missed?
Should I clean the html code (like remove invalid tags for example) before passing it to the reader?
What do you consider is the best way to display the pages? Should I use CSS and the 'column' property? Or should I make a more advanced script that will split the html content of a chapter into pages?
Thanks
First extract the .epub file, its same like your zip file so you can use PHP unzip library and dont need to parse HTML or CSS. You can create your reader using HTML5 canvas and CSS 3 properties.
I think better option is to use HTML5 and CSS3 if you are not thinking IE compatibility.

Extracting portion of the HTML page

Is it possible to extract a portion of a remote HTML page, and print it on another page, using PHP cURL, HTML DOM parser, or any other method, preserve the original formatting styles, images, tabs functioning?
For example, how to extract content of central column (with tabs and formatting, preserve the look of the original text), from http://ru.yahoo.com/?
As far as I understand, the script should process an external CSS, so that returned content has the same look as the original. What would be most appropriate way, if that's possible? If yes, an example would be highly appreciated. I looked several examples, but didn't find any solution for my case.
Well if I had to do it quickly (read: very dirty) I would do is this I think:
Pull the HTML from the remote server using standard PHP
Use the HTML that you stole took from the other site and add your own HTML to it down at the bottom.
Also add your own CSS to hide the html of the other site you don't want to be visible and style your own html.
Fiddle until it look okay enough. However: I think this will break the loading of the external JS files because of the same domain policy.
A nice approach would be this:
Pull the HTML from the remote server using standard PHP
Parse the HTML with some PHP HTML parser and strip out all external CSS and JS files and pull those files as well.
Use XPath to extract the parts that you need.
Create a new HTML document with your own HTML, the parts that you need, new links to your newly downloaded CSS and JS files. Also add your own CSS and JS to style the result.
You know: RSS was invented for this and if they don't provide an rss feed they most likely don't want you to get the content and post it on your own site. :P

PHP : Lookup all CSS files for content

My requirement is to lookup all the CSS content (external, internal and inline) from a given URL for some specific CSS content. I am currently using the 'PHP Simple HTML DOM Parser' to lookup HTML. But is there a specific way I can achieve this for CSS, specifically, all types of CSS ?
Thanks in advance :)
It is possible to gather all CSS a HTML document includes (style attributes and elements) and links (link element, type CSS).
However, as far as I know, some CSS parser in PHP exists so far (but no interface to CSSOM). So you would be able to gather all CSS, but you would need to write/integrate a parser for CSS on your own.
Keep in mind that some sites use things like less which is then turned into CSS via javascript, so you might want to integrate less into PHP as well. Same applies for other, client-side CSS technologies.

php scraping HTML - problems with IE only

I am scraping a website with HTML with php that retrieves a page and removes certain elements to only show a photo gallery. It works flawlessly for every browser BUT any version of IE (typical ;)). We can fix the problem by rewriting the .css file, but we cannot implement it into the head of the php as this will be overwritten by the .css file from the websites server. How would we go about hosting our own version of the .css file so that our website will be displayed using OUR version? Would be swap something out with a filter?
Cheers!
You do realize that it may not really be a scraping problem? It's sounds like a straightforward page display problem.
Worrying about scraping might be a red herring. After you have scraped you have some HTML (and possibly some CSS) ... does that validate at W3C? I realize that is no guarantee, but it is an indicator (I know that IE doesn't always display valid pages properly, but sometimes it's a "gotcha" when other browsers seem to display invalid HTML/CSS properly).
If it's valid then maybe you should look back at your scraping. If you already removes certain elements to only show a photo gallery then maybe you can also remove the CSS from the HTML header (or wherever) and reaplce it with your own?
If you're already scrapping the website, why not just use PHP to omit their CSS file and write your own in its place? Alternatively, you could write your own CSS file just below their's in the <head> so it overwrote their styles.
This is just another thing to check, but if one of the elements you're removing is comments, you could unwittingly be pulling out ie only stylesheets that are between conditional comments. Another thing to look at is paths. Maybe one of their stylesheets has a relative path that you can't call from your server. You would need to make that an absolute path for it to work.
Really, you should probably take a close look at the source of the original page and your formatted source side by side. You could be pulling out something that's should be left in.
You ask how you could remove their css... you do it the same way you remove the other elements you're pulling out. Just pull out style tags and tags that link to stylesheets.
Aside from that I would just write some styles to fix it and stick it them anywhere after the existing css is called. (Like every one else here mentioned)
Just add another CSS header and mark your styles as !important to override the original ones?

Categories