Creating a personalization engine with php - php

I am new to php and I want to create an php engine which changes the web content of a webpage with PHP with the use of data in mysql. For example (changing the order of navigation links on a webpage with the order of highest click count) I am not sure how PHP will read the HTML file and change the elements in the HTML file and also output the HTML file with the changes. Is this possible?

I am not quite sure why you would want to generate the html, read it, change it and then output it. It seems to be a lot easier to just generate it the way you want to in the first place.
I am not sure how PHP will read the HTML file and change the elements in the HTML file and also output the HTML file with the changes. Is this possible?
You could use file_get_contents:
$html = file_get_contents($url);
Then use a html-parser like Simple HTML DOM Parser, change what you want to do and output it.

If you want to modify HTML structure, use ganon - HTML DOM parser for PHP
include('path/ganon.php');
// Parse the google code website into a DOM
$html = file_get_dom('http://code.google.com/');
foreach($html('p[class]') as $element) {
echo $element->class, "<br>\n";
}

Related

What is a good way to change/replace elements in a php+css document?

Forgive me for possibly misusing certain terminology. I would like to:
open a html/php file, with php
find elements with certain class
change its innerHtml basically
than save the file.
I have a feeling DOMElement in php could help, since I've used it for similar things in javascript, but I am (still) unsure of its function in PHP and php.net sais a DOM document "Represents an entire HTML or XML document" (so no php/javascript containing document).
So: which function(s), libraries should I study to best perform those operations?
Possibly: Aside from php.net do you perhaps have a good tutorial for ^that^ solution?
Edit: Possibly related: Manipulate HTML from php
Edit2: If I would build something with my knowledge a.t.m. it would probably be almost as violent as solutions I tried earlier: https://stackoverflow.com/a/8960363/574700
There will be php in the document.
There will be some inline css and javascript in there.
1.open a html/php file, with php
Include htmlsimpledom and open you php file.
$html = file_get_html('myfile.html');
2.find elements with certain class
foreach($html -> find('.class-name') as $element)
$element - > plaintext. '<br>';
3.change its innerHtml basically
$html -> find('.class-name') -> innertext = 'text-here';
4.then save the file.
file_put_contents($filename, $html);

i want to get data from another website and display it on mine but with my style.css

So my school has this very annoying way to view my rooster.
you have to bypass 5 links to get to my rooster.
this is the link for my class (it updates weekly without changing the link)
https://webuntis.a12.nl/WebUntis/?school=roc%20a12#Timetable?type=1&departmentId=0&id=2147
i want to display the content from that page on my website but with my
own stylesheet.
i don't mean this:
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
or an iframe....
I think this can be better done using jquery and ajax. You can get jquery to load the target page, use selectors to strip out what you need, then attach it to your document tree. You should then be able to style it anyway you like.
I would recommend you to use the cURL library: http://www.php.net/manual/en/curl.examples.php
But you have to extract part of the page you want to display, because you will get the whole HTML document.
You'd probably read the whole page into a string variable (using file_get_contents like you mentioned for example) and parse the content, here you have some possibilities:
Regular expressions
Walking the DOM tree (eg. using PHPs DOMDocument classes)
After that, you'd most likely replace all the style="..." or class="..." information with your own.

PHP: Get all CSS files of an HTML web page

I'm trying to get all CSS files of an html file from URL.
I know that if I want to get the HTML code it is easy - just using PHP function - file_get_contents.
The question is - if I could search easily inside an a URL of HTML and get from there the files or content of all related CSS files?
Note - I want to build an engine for getting a lot of CSS files, this is why just reading the source is not enough..
Thanks,
You could try using http://simplehtmldom.sourceforge.net/ for HTML parsing.
require_once 'SimpleHtmlDom/simple_html_dom.php';
$url = 'www.website-to-scan.com';
$website = file_get_html($url);
// You might need to tweak the selector based on the website you are scanning
// Example: some websites don't set the rel attribute
// others might use less instead of css
//
// Some other options:
// link[href] - Any link with a href attribute (might get favicons and other resources but should catch all the css files)
// link[href="*.css*"] - Might miss files that aren't .css extension but return valid css (e.g.: .less, .php, etc)
// link[type="text/css"] - Might miss stylesheets without this attribute set
foreach ($website->find('link[rel="stylesheet"]') as $stylesheet)
{
$stylesheet_url = $stylesheet->href;
// Do something with the URL
}
You need to parse the HTML tags looking for CSS files. You can do it for example with preg_match - looking for matching regex.
Regex which would find such files might be like this:
\<link .+href="\..+css.+"\>

Extract all links which ends with .js extension in html page

I want to extract all links that ends with .js within html page.I am able to fetch links that are within script tag
but how could i fetch links from properties like {"yui":"http://l.yimg.com/nn/lib/metro/g/uicontrib/yui/yui_3.4.1.js"}.
I want this to be done in php
A simple PHP HTML DOM parser written in PHP5+, supports invalid HTML, and provides a very easy way to handle HTML elements. Find tags on an HTML page with selectors just like jQuery. Extract contents from HTML in a single line.
Here is the link to get it: http://sourceforge.net/projects/simplehtmldom/
...and here is the official web site: http://simplehtmldom.sourceforge.net/
For basic HTML elements you can use http://code.google.com/p/phpquery/ to parse DOM content (it handle jquery like CSS selectors, functions like attr, find). Here is example howto use selectors with PhpQuery http://code.google.com/p/phpquery/wiki/Selectors.
For properties, it depends:
Some kind of regexp if they are in Javascripts or something else,
If they are in data attributes and you know attributes name, then you can get that json string and simply run json_decode php function on it.

How to write this crawler in php?

I need to create a php script.
The idea is very simple:
When I send a link of a blogpost to this php script, then the webpage is crawled and the first image with the title page are saved on my server.
What PHP function I have to use for this crawler ?
Use PHP Simple HTML DOM Parser
// Create DOM from URL
$html = file_get_html('http://www.example.com/');
// Find all images
$images = array();
foreach($html->find('img') as $element) {
$images[] = $element->src;
}
Now $images array have images links of given webpage. Now you can store your desired image in database.
HTML Parser: HTMLSQL
Features: you can get external html file, http or ftp link and parse content.
Well, you'll have to use quite a few functions :)
But I'm going to assume that you're asking specifically about finding the image, and say that you should use a DOM parser like Simple HTML DOM Parser, then curl to grab the src of the first img element.
I would user file_get_contents() and a regular expression to extract the first image tags src attribute.
CURL or a HTML Parser seem overkill in this case, but you are welcome to check it out.

Categories