<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>枫子博客PHP</title>
	<atom:link href="http://www.imfeng.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.imfeng.com</link>
	<description>很枫的那种 闲人请勿靠近</description>
	<lastBuildDate>Sun, 05 Feb 2012 11:12:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP CURL_MULTI 多线程采集网页函数</title>
		<link>http://www.imfeng.com/php-curl-multi-function/</link>
		<comments>http://www.imfeng.com/php-curl-multi-function/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 13:19:47 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[CURL_MULTI]]></category>
		<category><![CDATA[多线程]]></category>
		<category><![CDATA[采集]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=852</guid>
		<description><![CDATA[这两天有一客户定制了一个免登录发布模块，因为在模块中需要涉及到很多图片下载的问题，考虑到性能问题，所以特别写了一个CURL_MULTI远程采集网页的函数，以方便以后使用，估计以后都不会使用原来的单线程curl函数去foreach了，其性能对比很明显的。同样获取我的博客的十个不同网页，curl_multi:4.5246081352234，file_get_contents:33.001797914505，将近8倍的效率，可想而知，如果在附件更多的情况下，性能差异就越明显了，希望对您有所帮助！


&#60;?php
$text = remote(array('http://www.imfeng.com/','http://www.aligaduo.com/'));
print_r($text);

function remote($urls) {
    if (!is_array($urls) or count($urls) == 0) {
        return false;
    }

    $curl = $text = array();
    $handle = curl_multi_init();
    foreach($urls as $k =&#62; $v) {
     [...]]]></description>
			<content:encoded><![CDATA[<p>这两天有一客户定制了一个免登录发布模块，因为在模块中需要涉及到很多图片下载的问题，考虑到性能问题，<span id="more-852"></span>所以特别写了一个CURL_MULTI远程采集网页的函数，以方便以后使用，估计以后都不会使用原来的单线程curl函数去foreach了，其性能对比很明显的。同样获取我的博客的十个不同网页，curl_multi:4.5246081352234，file_get_contents:33.001797914505，将近8倍的效率，可想而知，如果在附件更多的情况下，性能差异就越明显了，希望对您有所帮助！</p>
<pre class="brush: php; title: ;">

&lt;?php
$text = remote(array('http://www.imfeng.com/','http://www.aligaduo.com/'));
print_r($text);

function remote($urls) {
    if (!is_array($urls) or count($urls) == 0) {
        return false;
    }

    $curl = $text = array();
    $handle = curl_multi_init();
    foreach($urls as $k =&gt; $v) {
        $nurl[$k]= preg_replace('~([^:\/\.]+)~ei', &quot;rawurlencode('\\1')&quot;, $v);
        $curl[$k] = curl_init($nurl[$k]);
        curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl[$k], CURLOPT_HEADER, 0);
        curl_multi_add_handle ($handle, $curl[$k]);
    }

    $active = null;
    do {
        $mrc = curl_multi_exec($handle, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active &amp;&amp; $mrc == CURLM_OK) {
        if (curl_multi_select($handle) != -1) {
            do {
                $mrc = curl_multi_exec($handle, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    foreach ($curl as $k =&gt; $v) {
        if (curl_error($curl[$k]) == &quot;&quot;) {
        $text[$k] = (string) curl_multi_getcontent($curl[$k]);
        }
        curl_multi_remove_handle($handle, $curl[$k]);
        curl_close($curl[$k]);
    }
    curl_multi_close($handle);
    return $text;
}
</pre>
<p><a href="http://www.kalvin.cn/article/php-curl-multi-function/">原文地址</a></p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-curl-multi-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP采集类Snoopy伪造来源,IP</title>
		<link>http://www.imfeng.com/php-collection-class-snoopy-ip/</link>
		<comments>http://www.imfeng.com/php-collection-class-snoopy-ip/#comments</comments>
		<pubDate>Sat, 08 Oct 2011 15:44:40 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snoopy]]></category>
		<category><![CDATA[采集]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=846</guid>
		<description><![CDATA[在采集的时候经常会遇到被封IP，禁止采集。
这时候可以利用伪造搜索引擎蜘蛛UserAgent来突破限制实现采集。
一般情况下，枫子都是用Curl，并且已经习惯使用Curl了，还有一个专门用来采集的Curl函数，目前除了不能采集Google，其它的网站还没有发现不能采集的，不封是前提！
后来发现curl也有curl的缺点，于是开始研究php的采集类，著名的snoopy类。
下面是使用snoopy来突破限制的常用方法
1.使用Snoopy或curl传搜索引擎爬虫的USERAGENT值。
查看搜索引擎爬虫的USERAGENT值：http://www.imfeng.com/spdier-useragent/
2.使用Snoopy或curl传referer值。
如：$snoopy-&#62;referer = &#8216;http://www.imfeng.com/&#8216;;
$header[] = &#8220;Referer: http://www.imfeng.com/&#8220;;
3.使用Snoopy或curl代理。
如：$snoopy-&#62;proxy_host = &#8220;99.99.99.99&#8243;;
$snoopy-&#62;proxy_port = &#8220;3128&#8243;;
4.使用Snoopy或curl防造IP。
如：$snoopy-&#62;rawheaders['X_FORWARDED_FOR'] = &#8216;127.0.0.1&#8242;;
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p>在采集的时候经常会遇到被封IP，禁止采集。<span id="more-846"></span></p>
<p>这时候可以利用伪造<a href="http://www.imfeng.com/spdier-useragent/">搜索引擎蜘蛛UserAgent</a>来突破限制实现采集。</p>
<p>一般情况下，枫子都是用<a href="http://www.imfeng.com/tag/curl/">Curl</a>，并且已经习惯使用<a href="http://www.imfeng.com/tag/curl/">Curl</a>了，还有一个专门用来采集的Curl函数，目前除了不能采集Google，其它的网站还没有发现不能采集的，不封是前提！</p>
<p>后来发现curl也有curl的缺点，于是开始研究php的采集类，著名的snoopy类。</p>
<p>下面是使用snoopy来突破限制的常用方法</p>
<p>1.使用Snoopy或curl传搜索引擎爬虫的USERAGENT值。<br />
查看搜索引擎爬虫的USERAGENT值：<a href="http://www.imfeng.com/spdier-useragent/">http://www.imfeng.com/spdier-useragent/</a></p>
<p>2.使用Snoopy或curl传referer值。<br />
如：$snoopy-&gt;referer = &#8216;<a href="http://www.imfeng.com/">http://www.imfeng.com/</a>&#8216;;<br />
$header[] = &#8220;Referer: <a href="http://www.imfeng.com/">http://www.imfeng.com/</a>&#8220;;</p>
<p>3.使用Snoopy或curl代理。<br />
如：$snoopy-&gt;proxy_host = &#8220;99.99.99.99&#8243;;<br />
$snoopy-&gt;proxy_port = &#8220;3128&#8243;;</p>
<p>4.使用Snoopy或curl防造IP。<br />
如：$snoopy-&gt;rawheaders['X_FORWARDED_FOR'] = &#8216;127.0.0.1&#8242;;</p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-collection-class-snoopy-ip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php插入mysql数据避免重复插入</title>
		<link>http://www.imfeng.com/php-insert-into-the-data-to-avoid-duplication/</link>
		<comments>http://www.imfeng.com/php-insert-into-the-data-to-avoid-duplication/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 06:57:20 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[重复]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=691</guid>
		<description><![CDATA[在很多时候，我们使用php插入mysql都会碰到重复判断的情况，如果把需要插入的内容与数据库中所有内容进行对比再插入，效率将会变的很低。
如果使用mysql中的唯一索引，就可以很方便的实现去重插入。具体方法如下：
1，在phpmyadmin中设置一下需要去重的表字段：ALTER TABLE `test` ADD UNIQUE(`uid`)
其中，list是表名，uid是字段名。
2，在php程序中使用ignore方式插入：
mysql_query(“INSERT ignore INTO list (uid,con)
VALUES (‘$hy’,&#8217;$nr’)”);
这样就能很方便的实现php插入mysql数据库避免重复插入，但是要注意一点：如果需要去重的字段是text型，就会出现问题，因为mysql是不支持text型字段作为唯一索引的。所以需要将text型变为varchar，但是如果字段字节大于255个字节，次方法就会失效。
原文:http://www.semxian.com/blog/?p=50
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p>在很多时候，我们使用php插入mysql都会碰到重复判断的情况，如果把需要插入的内容与数据库中所有内容进行对比再插入，效率将会变的很低。<span id="more-691"></span></p>
<p>如果使用mysql中的唯一索引，就可以很方便的实现去重插入。具体方法如下：</p>
<p>1，在phpmyadmin中设置一下需要去重的表字段：ALTER TABLE `test` ADD UNIQUE(`uid`)</p>
<p>其中，list是表名，uid是字段名。</p>
<p>2，在php程序中使用ignore方式插入：</p>
<p>mysql_query(“INSERT ignore INTO list (uid,con)<br />
VALUES (‘$hy’,&#8217;$nr’)”);</p>
<p>这样就能很方便的实现<strong>php插入mysql数据库避免重复插入</strong>，但是要注意一点：如果需要去重的字段是text型，就会出现问题，因为mysql是不支持text型字段作为唯一索引的。所以需要将text型变为varchar，但是如果字段字节大于255个字节，次方法就会失效。</p>
<p>原文:<a href="http://www.semxian.com/blog/?p=50">http://www.semxian.com/blog/?p=50</a></p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-insert-into-the-data-to-avoid-duplication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fatal error: Call to undefined function curl_init() 解决办法</title>
		<link>http://www.imfeng.com/fatal-error-call-to-undefined-function-curl_init/</link>
		<comments>http://www.imfeng.com/fatal-error-call-to-undefined-function-curl_init/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 17:38:24 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Fatal error]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=654</guid>
		<description><![CDATA[很奇怪,本来好好的，突然出现这个问题Fatal error: Call to undefined function curl_init()。
解决办法，弄一个phpinfo(); 看第7行，Loaded Configuration File 这个的意思是php.ini的位置。
然后编辑php.ini 把里面的extension=php_curl.dll注释去掉。然后重启。
当然，我这个问题肯定不是这样解决的，因为我装环境的时候就已经开启了。
我把php目录下面的libeay32.dll、ssleay32.dll文件复制到C:\WINDOWS 解决。
突然发现这和PHP使用curl遭遇IIS 500错误 冲突了。哈哈哈
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p>很奇怪,本来好好的，突然出现这个问题Fatal error: Call to undefined function curl_init()。<span id="more-654"></span></p>
<p>解决办法，弄一个phpinfo(); 看第7行，Loaded Configuration File 这个的意思是php.ini的位置。</p>
<p>然后编辑php.ini 把里面的extension=php_curl.dll注释去掉。然后重启。</p>
<p>当然，我这个问题肯定不是这样解决的，因为我装环境的时候就已经开启了。</p>
<p>我把php目录下面的libeay32.dll、ssleay32.dll文件复制到C:\WINDOWS 解决。</p>
<p>突然发现这和<a title="PHP使用curl遭遇IIS 500错误" href="http://www.imfeng.com/php-use-curl-iis-500-error/">PHP使用curl遭遇IIS 500错误</a> 冲突了。哈哈哈</p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/fatal-error-call-to-undefined-function-curl_init/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>利用PHP木马进行程序二次开发</title>
		<link>http://www.imfeng.com/using-php-trojan-to-secondary-development/</link>
		<comments>http://www.imfeng.com/using-php-trojan-to-secondary-development/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 06:47:53 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[二次开发]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=516</guid>
		<description><![CDATA[当然，也可以用别的工具。
只是用来查找哪些文件里面包含你需要修改的关键词。
找了N多国产木马，没有一个有效果的，呃，来一个俄罗斯的。
下载
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p>当然，也可以用别的工具。</p>
<p>只是用来查找哪些文件里面包含你需要修改的关键词。</p>
<p>找了N多国产木马，没有一个有效果的，呃，来一个俄罗斯的。</p>
<p><a href="http://www.imfeng.com/upload/PHPJackal.rar">下载</a></p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/using-php-trojan-to-secondary-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP判断是否中文</title>
		<link>http://www.imfeng.com/php-string-whether-chinese/</link>
		<comments>http://www.imfeng.com/php-string-whether-chinese/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 14:23:16 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=498</guid>
		<description><![CDATA[很简单&#8230;
if(preg_match(&#8221;/^[\x7f-\xff]+$/&#8221;, $str)){
}
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p>很简单&#8230;<br />
if(preg_match(&#8221;/^[\x7f-\xff]+$/&#8221;, $str)){</p>
<p>}</p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-string-whether-chinese/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php实现动转静生成纯静态页面的方法</title>
		<link>http://www.imfeng.com/php-dynamic-to-static-pages/</link>
		<comments>http://www.imfeng.com/php-dynamic-to-static-pages/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 06:03:53 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[静态]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=473</guid>
		<description><![CDATA[在php中实现动转静生成纯静态页面的方法不管用什么方法，原理都是一样的。就是用程序读取相应的数据来替换模版中的变量，然后生成静态页。php中主要用到的就是要用到fread()和fwirte()。而静态页面生成了之后，就会牵扯到修改的问题。这里可以用到正则匹配的方法来替换模版中改变的部 位。推荐的方法是直接把原来生成的模版砍掉，重新生成，呵呵，真正的一了百了。
还需要说明的一点就是，这种生成静态页面的方法一般都用于那些变化不是很频繁的页面，比如信息的最终页面。而针对列表页，如果信息更新不是很频繁的 话，也是可取的。现在网上流行好多可以生成静态页面的blog或者论坛程序，都是通过手动点击后台“生成html页”的按钮来“半自动”生成html的。 而对一些信息量非常大的门户网站，则行不通。因为静态页之所以叫“静态”，是因为其不可自动改变。如果信息列表每天更新100次，那么静态的列表页就要重 新生成100次。如果我有10个这样的栏目，那想想也够吐血的了。
好了，闲话少说，现在来看看实际的程序演示：
first：是一个利用ob函数来做的咚咚，代码比较简单，效率相对也高一些。
PHP代码

&#60;?php
ob_start();
@readfile(&#34;http://www.phphi.cn/post/186&#34;);
$text = ob_get_flush();
$myfile = fopen(&#34;myfile.html&#34;,&#34;w&#34;);
$text = str_replace (&#34;{counent}&#34;,$string,$text);
fwrite($myfile,$text);
ob_clean();
?&#62;
因为就算要生成静态页面，动态读取那部分也是要保留的，把数据插入数据库后，把url传递给readfile函数，然后读入缓存，fwrite一下 就可以生成静态页面，这种作法。代码行数最少，效率最高。http://www.phphi.cn/post/186/是一个裸页，也就是单纯的内容，没有头，尾，菜单。这样才能比较自由的定制自己的模版myfile.html。如果仅仅是要求生成静态页的话。
PHP代码

ob_start();
@readfile(&#34;http://www.phphi.cn/post/186&#34;);
$string = ob_get_flush();
$myfile = fopen(&#34;myfile.html&#34;,&#34;w&#34;);
fwrite($myfile,$string);
ob_clean();

就可以over了
second：普通生成静态html页。
这种作法就是按部就班的来做，fread进来页面，然后str_replace替换
首先是创建最终内容页：
PHP代码

$title = &#34;http://www.phphi.cn/post/186 &#124; phphi's blog&#34;;
$file = &#34;phphi's blog-是一个以php+jquery为中心，面向软件开发者、程序爱好者的开源技术型博客。&#34;;
$fp = fopen (&#34;temp.html&#34;,&#34;r&#34;);
$content = fread ($fp,filesize (&#34;temp.html&#34;));
$content = str_replace (&#34;{file}&#34;,$file,$content);
$content = str_replace (&#34;{title}&#34;,$title,$content);
$filename = &#34;test/test.html&#34;;
$handle = fopen ($filename,&#34;w&#34;); //打开文件指针，创建文件
/* 检查文件是否被创建且可写 */
if (!is_writable ($filename)) {
die (&#34;文件：&#34;.$filename.&#34;不可写，请检查其属性后重试！&#34;);
}
if (!fwrite ($handle,$content)){ //将信息写入文件
die (&#34;生成文件&#34;.$filename.&#34;失败！&#34;);
}
fclose ($handle); [...]]]></description>
			<content:encoded><![CDATA[<p>在php中实现动转静生成纯静态页面的方法不管用什么方法，原理都是一样的。<span id="more-473"></span>就是用程序读取相应的数据来替换模版中的变量，然后生成静态页。php中主要用到的就是要用到fread()和fwirte()。而静态页面生成了之后，就会牵扯到修改的问题。这里可以用到正则匹配的方法来替换模版中改变的部 位。推荐的方法是直接把原来生成的模版砍掉，重新生成，呵呵，真正的一了百了。</p>
<p>还需要说明的一点就是，这种生成静态页面的方法一般都用于那些变化不是很频繁的页面，比如信息的最终页面。而针对列表页，如果信息更新不是很频繁的 话，也是可取的。现在网上流行好多可以生成静态页面的blog或者论坛程序，都是通过手动点击后台“生成html页”的按钮来“半自动”生成html的。 而对一些信息量非常大的门户网站，则行不通。因为静态页之所以叫“静态”，是因为其不可自动改变。如果信息列表每天更新100次，那么静态的列表页就要重 新生成100次。如果我有10个这样的栏目，那想想也够吐血的了。</p>
<p>好了，闲话少说，现在来看看实际的程序演示：</p>
<p>first：是一个利用ob函数来做的咚咚，代码比较简单，效率相对也高一些。</p>
<p>PHP代码</p>
<pre class="brush: php; title: ;">
&lt;?php
ob_start();
@readfile(&quot;http://www.phphi.cn/post/186&quot;);
$text = ob_get_flush();
$myfile = fopen(&quot;myfile.html&quot;,&quot;w&quot;);
$text = str_replace (&quot;{counent}&quot;,$string,$text);
fwrite($myfile,$text);
ob_clean();
?&gt;</pre>
<p>因为就算要生成静态页面，动态读取那部分也是要保留的，把数据插入数据库后，把url传递给readfile函数，然后读入缓存，fwrite一下 就可以生成静态页面，这种作法。代码行数最少，效率最高。http://www.phphi.cn/post/186/是一个裸页，也就是单纯的内容，没有头，尾，菜单。这样才能比较自由的定制自己的模版myfile.html。如果仅仅是要求生成静态页的话。</p>
<p>PHP代码</p>
<pre class="brush: php; title: ;">
ob_start();
@readfile(&quot;http://www.phphi.cn/post/186&quot;);
$string = ob_get_flush();
$myfile = fopen(&quot;myfile.html&quot;,&quot;w&quot;);
fwrite($myfile,$string);
ob_clean();
</pre>
<p>就可以over了</p>
<p>second：普通生成静态html页。</p>
<p>这种作法就是按部就班的来做，fread进来页面，然后str_replace替换</p>
<p>首先是创建最终内容页：</p>
<p>PHP代码</p>
<pre class="brush: php; title: ;">
$title = &quot;http://www.phphi.cn/post/186 | phphi's blog&quot;;
$file = &quot;phphi's blog-是一个以php+jquery为中心，面向软件开发者、程序爱好者的开源技术型博客。&quot;;
$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;);
$content = fread ($fp,filesize (&quot;temp.html&quot;));
$content = str_replace (&quot;{file}&quot;,$file,$content);
$content = str_replace (&quot;{title}&quot;,$title,$content);
$filename = &quot;test/test.html&quot;;
$handle = fopen ($filename,&quot;w&quot;); //打开文件指针，创建文件
/* 检查文件是否被创建且可写 */
if (!is_writable ($filename)) {
die (&quot;文件：&quot;.$filename.&quot;不可写，请检查其属性后重试！&quot;);
}
if (!fwrite ($handle,$content)){ //将信息写入文件
die (&quot;生成文件&quot;.$filename.&quot;失败！&quot;);
}
fclose ($handle); //关闭指针
die (&quot;创建文件&quot;.$filename.&quot;成功！&quot;);
</pre>
<p>这一步比较简单。只是单纯的变量替换即可。如果要生成静态的列表页面的话，原理也是一样，用程序来生成文章列表，把它当成一个大的变量，替换模版中 的变量，列表的翻页页是如此。当然，如果有信息更新的话，列表翻页也是要重新生成的。</p>
<p>PHP代码</p>
<pre class="brush: php; title: ;">
&lt;?php
$title = &quot;http://www.phphi.cn/post/186&quot;;
$file = &quot;phphi's blog-是一个以php+jquery为中心，面向软件开发者、程序爱好者的开源技术型博客。&quot;;
$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;);
$content = fread ($fp,filesize (&quot;temp.html&quot;));
$content = str_replace (&quot;{file}&quot;,$file,$content);
$content = str_replace (&quot;{title}&quot;,$title,$content);
// 生成列表开始
$list = '';
$sql = &quot;select id,title,filename from article&quot;;
$query = mysql_query ($sql);
while ($result = mysql_fetch_array ($query)){
$list .= '&lt;a href='.$root.$result['filename'].' target=_blank&gt;'.$result['title'].'&lt;/a&gt;&lt;br&gt;'; }
$content .= str_replace (&quot;{articletable}&quot;,$list,$content); //生成列表结束
// echo $content;
$filename = &quot;test/test.html&quot;;
$handle = fopen ($filename,&quot;w&quot;); //打开文件指针，创建文件
/* 检查文件是否被创建且可写 */
if (!is_writable ($filename)){
die (&quot;文件：&quot;.$filename.&quot;不可写，请检查其属性后重试！&quot;);
}
if (!fwrite ($handle,$content)){ //将信息写入文件
die (&quot;生成文件&quot;.$filename.&quot;失败！&quot;);
}
fclose ($handle); //关闭指针
die (&quot;创建文件&quot;.$filename.&quot;成功！&quot;);
?&gt;
</pre>
<p>关于翻页：</p>
<p>如我们指定分页时，每页20篇。某子频道列表内文章经数据库查询为45条，则，首先我们通过查询得到如下参数：1，总 页数；2，每页篇数。第二步，for ($i = 0; $i < allpages; $i++)，页面元素获取，分析，文章生成，都在此循环中执行。不同的是，die ("创建文件".$filename."成功！";这句去掉，放到循环后的显示，因为该语句将中止程序执行。例：</p>
<p>PHP代码</p>
<pre class="brush: php; title: ;">
&lt;?php
$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;);
$content = fread ($fp,filesize (&quot;temp.html&quot;));
$onepage = &#8216;20&#8242;;
$sql = &quot;select id from article where channel=&#8217;$channelid&#8217;&quot;;
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$i&lt;$allpages; $i++)
{ if ($i == 0){ $indexpath = &quot;index.html&quot;; }
else { $indexpath = &quot;index_&quot;.$i.&quot;html&quot;; }
$start = $i * $onepage; $list = &#8221;;
$sql_for_page = &quot;select name,filename,title from article where channel=&#8217;$channelid&#8217; limit $start,$onepage&quot;;
$query_for_page = mysql_query ($sql_for_page);
while ($result = $query_for_page){ $list .= &#8216;&lt;a href=&#8217;.$root.$result['filename'].&#8217; target=_blank&gt;&#8217;.$title.&#8217;&lt;/a&gt;&lt;br&gt;&#8217;; }
$content = str_replace (&quot;{articletable}&quot;,$list,$content);
if (is_file ($indexpath)){
@unlink ($indexpath); //若文件已存在，则删除
}
$handle = fopen ($indexpath,&quot;w&quot;); //打开文件指针，创建文件
/* 检查文件是否被创建且可写 */
if (!is_writable ($indexpath)){ echo &quot;文件：&quot;.$indexpath.&quot;不可写，请检查其属性后重试！&quot;; //修改为echo
}
if (!fwrite ($handle,$content)){ //将信息写入文件 echo &quot;生成文件&quot;.$indexpath.&quot;失败！&quot;; //修改为echo
}
fclose ($handle); //关闭指针
}
fclose ($fp); die (&quot;生成分页文件完成，如生成不完全，请检查文件权限系统后重新生成！&quot;);
?&gt;
</pre>
<p>third：smarty模版生成静态页面<br />
smarty自己有一个fetch函数，其功用有点类似于fread()可以用来生成静态的页面.<br />
这个例子大家想必 看起来眼熟，对，smarty手册中关于fetch函数的例子，hoho 某驼借用一下，比竟官方的例子总是很经典的嘛！</p>
<p>PHP代码</p>
<pre class="brush: php; title: ;">
&lt;?php
include(&quot;Smarty.class.php&quot;);
$smarty = new Smarty;
$smarty-&gt;caching = true;
// only do db calls if cache doesn't exist if(!$smarty-&gt;is_cached(&quot;index.tpl&quot;)) {
// dummy up some data $address = &quot;245 N 50th&quot;;
$db_data = array(
&quot;City&quot; =&gt; &quot;Lincoln&quot;,
&quot;State&quot; =&gt; &quot;Nebraska&quot;,
&quot;Zip&quot; =&gt; &quot;68502&quot;
);
$smarty-&gt;assign(&quot;Name&quot;,&quot;Fred&quot;);
$smarty-&gt;assign(&quot;Address&quot;,$address);
$smarty-&gt;assign($db_data); }
// capture the output $output = $smarty-&gt;fetch(&quot;index.tpl&quot;);
//这个地方算是关键// do something with $output here echo $output; //hoho
//看到output的结果了吧
//然后呢?fwrite一下，我们就 得到我们所要的结果了。
$fp = fopen(&quot;archives/2005/05/19/0001.html&quot;, &quot;w&quot;);
fwrite($fp, $content);fclose($fp);
?&gt;
&lt;?phpob_start();echo &quot;Hello World!&quot;;
$content = ob_get_contents();//取得php页面输出的全部内容
$fp = fopen(&quot;archives/2005/05/19/0001.html&quot;, &quot;w&quot;);
fwrite($fp, $content);
fclose($fp);
?&gt;
</pre>
<p>PHP代码<br />
//PHP生成静态页面类 </p>
<pre class="brush: php; title: ;">
&lt;?php
//-----------------------------生成静态的类-------------------------------
class Makehtml{
public $MbUrl,$OutUrl,$AllHtml,$SouChar,$ObjChar; //变量
public $row; //游标
public $Shuzusou,$Shuzuobj; //替换的字符串数组
//-----------------------初始化-------------------------
function __construct(){ //初始化
$this-&gt;MbUrl=&quot;&quot;;
$this-&gt;OutUrl=&quot;&quot;;
$this-&gt;AllHtml=&quot;&quot;;
$this-&gt;Sql=&quot;&quot;;
$this-&gt;SouChar=&quot;&quot;;
$this-&gt;ObjChar=&quot;&quot;;
}
//-----------------------------自动按字段替换---------------------------
function AutoReplace(){
//------------------自动获取要替换的字符串-------------------
$tlen=count($row);
$shuzu1=array();
$shuzu2=array();
if($row){
$i=0;
foreach($row as $key =&gt; $value){
$shuzu2=&quot;&lt;=\$&quot;.$key.&quot;\$&gt;&quot;;
$shuzu1=$value;
$i++;
}
$this-&gt;Replacehtml(shuzu2,shuzu1);
}
//------------------自动获取要替换的字符串-------------------
}
//-----------------------------自动按字段替换完成------------------------
//-----------------------------批量替换数组--------------------------
function Replacehtml($Shuzusou,$Shuzuobj){ //批量替换数组
if(count($Shuzusou)!=count($Shuzuobj)){
exit(&quot;替换数组不匹配&quot;);
}
if($this-&gt;AllHtml==&quot;&quot;){
exit(&quot;没有要替换的内容&quot;);
}
for($i=0;$i&lt;count($Shuzusou);$i++){
$this-&gt;AllHtml=str_replace($Shuzusou[$i],$Shuzuobj[$i],$this-&gt;AllHtml);
//print(&quot;&lt;br&gt;&quot;.$Shuzusou(i).&quot;=&quot;.$Shuzuobj(i).&quot;&lt;br&gt;&quot;)
}
} //-----------------------------批量替换数组完成--------------------------
//-----------------------------读取文件---------------------------------
function Readfile(){
$file=fopen($this-&gt;MbUrl,&quot;r&quot;);
$fsize=filesize($this-&gt;MbUrl);
$this-&gt;AllHtml=fread($file,$fsize);
fclose($file);
}
//-----------------------------读取文件完成------------------------------
//-----------------------------保存文件---------------------------------
function SaveFile(){
$file=fopen($this-&gt;OutUrl,&quot;w&quot;);
fwrite($file,$this-&gt;AllHtml);
fclose($file);
}
//-----------------------------保存文件完成------------------------------
}
//------------------------------生成静态的类完成 -------------------------------
//以下为引用的内容：
//------------------静态生成----
$MyMake=new Makehtml;
$MyMake-&gt;MbUrl=&quot;demo.html&quot;;
$MyMake-&gt;Readfile();
$THTml=$MyMake-&gt;AllHtml;
$shuzu1=array();
$shuzu2=array();
$shuzu1[0]=&quot;{keywords}&quot;;
$shuzu1[1]=&quot;{description}&quot;;
$shuzu1[2]=&quot;{title}&quot;;

$shuzu1[3]=&quot;width=\&quot;100%\&quot;&quot;;
$MyMake-&gt;OutUrl=&quot;News_&quot;.date('m_s').&quot;.html&quot;;
$shuzu2[0]=&quot;新闻,视频&quot;;
$shuzu2[1]=&quot;本网站是一个...&quot;;
$shuzu2[2]=&quot;网站标题&quot;;

$shuzu2[3]=&quot;width=\&quot;95%\&quot;&quot;;
$MyMake-&gt;Replacehtml($shuzu1,$shuzu2);
$MyMake-&gt;SaveFile();
//------------------静态生成完成-----------
?&gt;
</pre>
<p>本篇文章来源于phphi 文章地址：http://www.phphi.cn/post/196/</p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-dynamic-to-static-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php随机生成备案号</title>
		<link>http://www.imfeng.com/php-get-rand-beianhao/</link>
		<comments>http://www.imfeng.com/php-get-rand-beianhao/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 13:48:02 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[随机]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=469</guid>
		<description><![CDATA[又有5天没见面了&#8230;哎。
miss u.

function GetBeianhao(){
	$o = '京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川黔云藏陕甘青宁新港澳台';
	$output = '';
 	for($a=0;$a&#60;8;$a++){
  		$output.=mt_rand(0,9);
 	}
	$beianhao = substr($o, rand(1, strlen($o)/2)*2, 2).&#34;ICP备&#34;.$output.&#34;号&#34;;
 	return $beianhao;
}
echo GetBeianhao();

有点简单 凑合着用吧
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p>又有5天没见面了&#8230;哎。</p>
<p>miss u.<span id="more-469"></span></p>
<pre class="brush: php; title: ;">
function GetBeianhao(){
	$o = '京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川黔云藏陕甘青宁新港澳台';
	$output = '';
 	for($a=0;$a&lt;8;$a++){
  		$output.=mt_rand(0,9);
 	}
	$beianhao = substr($o, rand(1, strlen($o)/2)*2, 2).&quot;ICP备&quot;.$output.&quot;号&quot;;
 	return $beianhao;
}
echo GetBeianhao();
</pre>
<p>有点简单 凑合着用吧</pre>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-get-rand-beianhao/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php.ini文件中的error_reporting参数</title>
		<link>http://www.imfeng.com/php-ini-error_reporting/</link>
		<comments>http://www.imfeng.com/php-ini-error_reporting/#comments</comments>
		<pubDate>Tue, 25 Jan 2011 12:07:20 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[error_reporting]]></category>
		<category><![CDATA[php.ini]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=433</guid>
		<description><![CDATA[error_reporting可以设置的参数如下：
错误报告是位字段。可以将数字加起来得到想要的错误报告等级。
E_ALL &#8211; 所有的错误和警告(不包括 E_STRICT)
 E_ERROR &#8211; 致命性的运行时错误
 E_WARNING &#8211; 运行时警告(非致命性错误)
 E_PARSE &#8211; 编译时解析错误
 E_NOTICE &#8211; 运行时提醒(这些经常是你代码中的bug引起的，也可能是有意的行为造成的。)
 E_STRICT &#8211; 编码标准化警告，允许PHP建议如何修改代码以确保最佳的互操作性向前兼容性。
 E_CORE_ERROR &#8211; PHP启动时初始化过程中的致命错误
 E_CORE_WARNING &#8211; PHP启动时初始化过程中的警告(非致命性错)
 E_COMPILE_ERROR &#8211; 编译时致命性错
 E_COMPILE_WARNING &#8211; 编译时警告(非致命性错)
 E_USER_ERROR &#8211; 用户自定义的错误消息
 E_USER_WARNING &#8211; 用户自定义的警告消息
 E_USER_NOTICE &#8211; 用户自定义的提醒消息
 例子:
error_reporting = E_ALL &#38; ~E_NOTICE ; 显示所有的错误，除了提醒
error_reporting = E_COMPILE_ERROR&#124;E_ERROR&#124;E_CORE_ERROR ; 仅显示错
error_reporting=E_ERROR :只会报告致命性错误
基本上设置
error_reporting = E_ALL &#38; ~E_NOTICE ; 显示所有的错误，除了提醒
Here is no comments yet by the time  your rss reader [...]]]></description>
			<content:encoded><![CDATA[<p>error_reporting可以设置的参数如下：<span id="more-433"></span></p>
<p>错误报告是位字段。可以将数字加起来得到想要的错误报告等级。<br />
E_ALL &#8211; 所有的错误和警告(不包括 E_STRICT)<br />
 E_ERROR &#8211; 致命性的运行时错误<br />
 E_WARNING &#8211; 运行时警告(非致命性错误)<br />
 E_PARSE &#8211; 编译时解析错误<br />
 E_NOTICE &#8211; 运行时提醒(这些经常是你代码中的bug引起的，也可能是有意的行为造成的。)<br />
 E_STRICT &#8211; 编码标准化警告，允许PHP建议如何修改代码以确保最佳的互操作性向前兼容性。<br />
 E_CORE_ERROR &#8211; PHP启动时初始化过程中的致命错误<br />
 E_CORE_WARNING &#8211; PHP启动时初始化过程中的警告(非致命性错)<br />
 E_COMPILE_ERROR &#8211; 编译时致命性错<br />
 E_COMPILE_WARNING &#8211; 编译时警告(非致命性错)<br />
 E_USER_ERROR &#8211; 用户自定义的错误消息<br />
 E_USER_WARNING &#8211; 用户自定义的警告消息<br />
 E_USER_NOTICE &#8211; 用户自定义的提醒消息<br />
 例子:<br />
error_reporting = E_ALL &amp; ~E_NOTICE ; 显示所有的错误，除了提醒<br />
error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR ; 仅显示错</p>
<p>error_reporting=E_ERROR :只会报告致命性错误</p>
<p>基本上设置</p>
<p>error_reporting = E_ALL &amp; ~E_NOTICE ; 显示所有的错误，除了提醒</p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/php-ini-error_reporting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>一年感悟之PHP</title>
		<link>http://www.imfeng.com/2010-with-php/</link>
		<comments>http://www.imfeng.com/2010-with-php/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 10:29:08 +0000</pubDate>
		<dc:creator>枫子</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SEO|互联网]]></category>
		<category><![CDATA[一年感悟]]></category>

		<guid isPermaLink="false">http://www.imfeng.com/?p=429</guid>
		<description><![CDATA[今天登陆后台无意间看到一个评论，http://www.imfeng.com/why-php/#comment-978 打开这篇文章 
为什么我就不会PHP呢？ 日期2010年1月12日的 已经过去一年时间了，PHP水平显著提升啊。哈哈
基本上二次开发都没有很大的问题，除非是做一些比较复杂的东西，简单的文章系统，和数据库打交道的，采集程序之类的都已经很熟练了,并且能破解除了zend加密的php代码，即使很复杂。
这一个过程其实是很简单的，主要是PHP100视频教程帮助了我，但是至今还没看完php100的全部教程，只是挑选了一些自己需要的东西学了学，基本上有问题，百度一下，都能解决的。
总结就是：多做小东西，多看代码。
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">今天登陆后台无意间看到一个评论，<a href="http://www.imfeng.com/why-php/#comment-978">http://www.imfeng.com/why-php/#comment-978</a> 打开这篇文章 <br />
<a title="为什么我就不会PHP呢？" rel="bookmark" href="http://www.imfeng.com/why-php/">为什么我就不会PHP呢？</a> 日期2010年1月12日的 已经过去一年时间了，PHP水平显著提升啊。哈哈<span id="more-429"></span></p>
<p>基本上二次开发都没有很大的问题，除非是做一些比较复杂的东西，简单的文章系统，和数据库打交道的，采集程序之类的都已经很熟练了,并且能破解除了zend加密的php代码，即使很复杂。</p>
<p style="text-align: left;">这一个过程其实是很简单的，主要是<a href="http://www.imfeng.com/php100-download-update-90/">PHP100视频教程</a>帮助了我，但是至今还没看完<a href="http://www.imfeng.com/tag/php100/">php100</a>的全部教程，只是挑选了一些自己需要的东西学了学，基本上有问题，百度一下，都能解决的。</p>
<p style="text-align: left;">总结就是：<span style="color: #ff0000;">多做小东西，多看代码</span>。</p>
Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.imfeng.com/2010-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

