PHP collects static pages and saves css, img, and js pages, and static page css.

Source: Internet
Author: User

PHP collects static pages and saves css, img, and js pages, and static page css.

This example describes how PHP collects static pages and saves css, img, and js pages. Share it with you for your reference. The specific analysis is as follows:

This is a small tool that can obtain html code and css, js, font, and img resources from webpages. It is mainly used to quickly obtain templates. If you cannot design the UI or see a good template, you can use this tool to capture webpages and extract resource files. The extracted content will be saved in the relative path, so you do not have to worry about wrong url import of resource files.

The code for index. php on the homepage is as follows:
Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Meta name = "author" content = "flute"/>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> webpage capture </title>
<Link rel = "stylesheet" href = "main.css" media = "all"/>
<Script type = "text/javascript" src = "jquery. js"> </script>
<Script type = "text/javascript" src = "main. js"> </script>
</Head>
<Body>
<H1> Web Grabber <Hr/>
<Div class = "box">
<H2> Url <Div class = "form">
<Input type = "text" id = "project" value = "projectname"/>
<Input type = "text" id = "url" value = "http: //" size = "60"/>
<Button class = "submit" type = "button"> Get </button> <span id = "tip"> </span>
</Div>
</Div>
<Div class = "box">
<Span class = "all" id = "saveall"> Save All </span>
<H2> List <Ul id = "list">
</Ul>
</Div>
</Body>
</Html>
Capture the Page code grab. php. The Code is as follows:
Copy codeThe Code is as follows: <? PHP
/*
* Flute
* March 31
*/
 
If (isset ($ _ POST ['url']) {
If (isset ($ _ POST ['project']) &! Is_dir ($ _ POST ['project']) mkdir ($ _ POST ['project'], 0777 );
Echo json_encode (grab ($ _ POST ['url']);
}
 
Function grab ($ url ){
// $ Url = 'HTTP: // ldixing-wordpress.stor.sinaapp.com/uploads/leaves/test.html ';
$ Data = array ();
$ File = preg_replace ('/^. * //', '', $ url );
 
If ($ content = file_get_contents ($ url ))! = False ){
 
If (isset ($ _ POST ['project']) file_put_contents ($ _ POST ['project']. '/'. $ file, $ content );
 
$ Pattern = '/<link .*? Href = ('| ")(.*?. Css) 1. *?> /I ';
If (preg_match_all ($ pattern, $ content, $ matches )){
$ Data ['css '] = $ matches [2];
}
 
$ Pattern = '/<script .*? Src = ('| ")(.*?. Js) 1. *?> /I ';
If (preg_match_all ($ pattern, $ content, $ matches )){
$ Data ['js'] = $ matches [2];
}
 
$ Pattern = '/ /I ';
If (preg_match_all ($ pattern, $ content, $ matches )){
$ Data ['img '] = $ matches [2];
}
 
$ Pattern = '/url (' | "| s )(.*?) 1)/I ';
If (preg_match_all ($ pattern, $ content, $ matches )){
$ Data ['src'] = $ matches [2];
}
}
 
Return $ data;
}

Function vardump ($ obj ){
Echo '<pre> ';
Print_r ($ obj );
Echo '</pre> ';
}
?>
Save. php, the Code is as follows:
Copy codeThe Code is as follows: <? PHP
/*
* Flute
*
*/
 
If (isset ($ _ POST ['url']) & isset ($ _ POST ['project']) & isset ($ _ POST ['domain ']) {
Extract ($ _ POST );
$ Url = preg_replace ('/?. * $/', '', $ Url );
$ File = $ url;
$ Arr = explode ('/', $ file );
$ Length = sizeof ($ arr );
$ Filename = $ arr [$ length-1];
$ Root = $ project;
$ Dir = $ root;
 
If ($ domain = 'HTTP '){
$ Dir = $ root. '/http ';
If (! Is_dir ($ dir) mkdir ($ dir, 0777 );
} Else {
$ File = $ domain. '/'. $ url;
For ($ I = 0; $ I <$ length-1; $ I ++ ){
If (! Emptyempty ($ arr [$ I]) {
$ Dir. = '/'. $ arr [$ I];
If (! Is_dir ($ dir) mkdir ($ dir, 0777 );
}
}
}
If (! File_exists ($ dir. '/'. $ filename) | filesize ($ dir. '/'. $ filename) = 0 ){
$ Content = file_get_contents ($ file );
File_put_contents ($ dir. '/'. $ filename, $ content );
}
}
?>
Usage:

1. Open the indexpage, enter the project name and the network to capture, and the network keyword is the end of the file name, such as index.html;

2. Click the Get button to obtain a list of all css, js, img, and other resources on the current page;

3. Click the css link to obtain the background resource image in the css file, which is appended to the back of the list;

4. Click Save All to Save All the files in the list and generate them according to the relative path;

5. If an http remote file exists on the webpage, it will be saved directly in the http folder;

6. sometimes Get and Save will fail. It doesn't matter how many retries.

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.