PHP class _php tutorial for generating static pages

Source: Internet
Author: User
Copy CodeThe code is as follows: Class HTML
{
var $dir; Dir for the htmls (without/)
var $rootdir; Root of HTML files (without/): HTML
var $name; HTML file storage Path
var $dirname; The specified folder name
var $url; Gets the source page address of the HTML file information
var $time; HTML file information time to fill in overtime
var $dirtype; Directory Storage method: Year,month,,,,
var $nametype; HTML file naming method: Name

function html ($nametype = ' name ', $dirtype = ' year ', $rootdir = ' html ')
{
$this->setvar ($nametype, $dirtype, $rootdir);
}

function Setvar ($nametype = ' name ', $dirtype = ' year ', $rootdir = ' html ')
{
$this->rootdir= $rootdir;
$this->dirtype= $dirtype;
$this->nametype= $nametype;
}

function Createdir ($dir = ")
{
$this->dir= $dir? $dir: $this->dir;

if (!is_dir ($this->dir))
{
$temp = explode ('/', $this->dir);
$cur _dir = ";
for ($i =0; $i {
$cur _dir. = $temp [$i]. ' /';
if (!is_dir ($cur _dir))
{
@mkdir ($cur _dir,0777);
}
}
}
}

function Getdir ($dirname = ", $time =0)
{
$this->time= $time? $time: $this->time;
$this->dirname= $dirname? $dirname: $this->dirname;

Switch ($this->dirtype)
{
Case ' name ':
if (Empty ($this->dirname))
$this->dir= $this->rootdir;
Else
$this->dir= $this->rootdir. ' /'. $this->dirname;
Break
Case ' Year ':
$this->dir= $this->rootdir. ' /'. Date ("Y", $this->time);
Break

Case ' Month ':
$this->dir= $this->rootdir. ' /'. Date ("Y-m", $this->time);
Break

Case ' Day ':
$this->dir= $this->rootdir. ' /'. Date ("y-m-d", $this->time);
Break
}

$this->createdir ();

return $this->dir;
}

function Geturlname ($url = ")
{
$this->url= $url? $url: $this->url;

$filename =basename ($this->url);
$filename =explode (".", $filename);
return $filename [0];
}

function Geturlquery ($url = ")
{
$this->url= $url? $url: $this->url;

$durl =parse_url ($this->url);
$durl =explode ("&", $durl [query]);
foreach ($durl as $surl)
{
$gurl =explode ("=", $surl);
$eurl []= $gurl [1];
}
Return Join ("_", $eurl);
}

function getname ($url = ", $time =0, $dirname =")
{
$this->url= $url? $url: $this->url;
$this->dirname= $dirname? $dirname: $this->dirname;
$this->time= $time? $time: $this->time;

$this->getdir ();

Switch ($this->nametype)
{
Case ' name ':
$filename = $this->geturlname (). HTM ';
$this->name= $this->dir. '/'. $filename;
Break

Case ' time ':
$this->name= $this->dir. '/'. $this->time. HTM ';
Break

Case ' query ':
$this->name= $this->dir. '/'. $this->geturlquery (). HTM ';
Break

Case ' Namequery ':
$this->name= $this->dir. '/'. $this->geturlname (). ' -'. $this->geturlquery (). HTM ';
Break

Case ' Nametime ':
$this->name= $this->dir. '/'. $this->geturlname (). ' -'. $this->time. ' HTM ';
Break

}
return $this->name;
}

function createhtml ($url = ", $time =0, $dirname =", $htmlname = ")
{
$this->url= $url? $url: $this->url;
$this->dirname= $dirname? $dirname: $this->dirname;
$this->time= $time? $time: $this->time;
The above guarantees that the variable is not repeatedly assigned to the class member
if (empty ($htmlname))
$this->getname ();
Else
$this->name= $dirname. ' /'. $htmlname; Get Name

$content =file ($this->url) or Die ("Failed to open the URL". $this->url. "!");;

Key Step---read $this->url with file


$content =join ("", $content);
$fp = @fopen ($this->name, "w") or Die ("Failed to open the file". $this->name. "!");
if (@fwrite ($FP, $content))
return true;
Else
return false;
Fclose ($FP);
}
Generate HTML by name

function deletehtml ($url = ", $time =0, $dirname =")
{
$this->url= $url? $url: $this->url;
$this->time= $time? $time: $this->time;

$this->getname ();

if (@unlink ($this->name))
return true;
Else
return false;
}

/**
* Function::d eletedir ()
* Delete Directory
* @param $file directory name (without/)
* @return
*/
function Deletedir ($file)
{
if (file_exists ($file))
{
if (Is_dir ($file))
{
$handle =opendir ($file);
while (false!== ($filename =readdir ($handle)))
{
if ($filename! = "." && $filename! = "...")
$this->deletedir ($file. " /". $filename);
}
Closedir ($handle);
RmDir ($file);
return true;
}else{
Unlink ($file);
}
}
}

}
?>

http://www.bkjia.com/PHPjc/313728.html www.bkjia.com true http://www.bkjia.com/PHPjc/313728.html techarticle Copy the code as follows: PHP classhtml {var$dir;//dirforthehtmls (without/) var$rootdir;//rootofhtmlfiles (without/): HTML var$ name;//html file storage Path var$dirname;//The specified text ...

  • 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.