PHP to collect static page and save the page Css,img,js method _php Skill

Source: Internet
Author: User
Tags mkdir

This article illustrates the method of PHP to collect static page and save the page Css,img,js. Share to everyone for your reference. The specific analysis is as follows:

This is a Web page of HTML code and Css,js,font and IMG Resources of the gadget, mainly used to quickly get the template, if you do not have the time to design the UI or see a good template, you can use this tool to crawl Web pages and extract resource files, the extracted content by the relative path to save resources , so you don't have to worry about the incorrect URL import of the resource file.

Home index.php, the code is as follows:

Copy Code code as follows:
<! DOCTYPE html>
<meta name= "Author" content= "Flute"/>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> web crawler </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>
<body>
<div class= "box" >
<div class= "Form" >
<input type= "text" id= "project" value= "ProjectName"/>
<input type= "text" id= "url" value= "http://" size= "a"/>
<button class= "Submit" type= "button" >get</button><span id= "Tip" ></span>
</div>
</div>
<div class= "box" >
<span class= "All" id= "SaveAll" >save all</span>
<ul id= "List" >
</ul>
</div>
</body>

Crawl the page code grab.php, the code is as follows:
Copy Code code as follows:
? Php
/*
* Flute
* 2014/03/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 css,js,img and other resources of the page save.php, the code is as follows:
Copy Code code as follows:
? Php
/*
* Flute
* 2014/03/31
*/

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);
}
}
?>

How to use:

1. Open the index page, enter the project name and the URL to crawl, the URL must be the end of the filename, such as index.html;

2. The dot get button, gets the current page all css,js,img and so on resource list;

3. Click the CSS link to get a picture of the background resources in the CSS file, attached to the back of the list;

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

5. If there are HTTP remote files on the Web page, they will be saved directly under the HTTP folder;

6. Get and save sometimes fail, it's OK to retry several times.

I hope this article will help you with your PHP program design.

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.