<?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>DALLASCAO.COM &#187; php</title>
	<atom:link href="http://dallascao.com/cn/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://dallascao.com/cn</link>
	<description>英汉翻译 Dallas Cao 的网站</description>
	<lastBuildDate>Mon, 23 Jan 2012 11:42:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>修改php和mysql的时区</title>
		<link>http://dallascao.com/cn/change-your-php-and-mysql-timezone/</link>
		<comments>http://dallascao.com/cn/change-your-php-and-mysql-timezone/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 16:11:03 +0000</pubDate>
		<dc:creator>Dallas Cao</dc:creator>
				<category><![CDATA[代码狂]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://dallascao.com/cn/?p=428</guid>
		<description><![CDATA[<p><code>date_default_timezone_set('PRC'); </code></p>
<p>把php环境时区改为中国 (+8)。</p>
<p>用数据的话还要使用</p>
<p><code>mysql_query("SET time_zone = '+8:00'");</code></p>
<p>改变mysql环境时区。</p>
<div id="crp_related"><h3>相关文章：</h3><ul><li><a href="http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">在 .htaccess 中使用正则表达式来定义转向</a></li><li><a href="http://dallascao.com/cn/%e7%82%b9%e5%87%bb%e5%8d%95%e5%85%83%e6%a0%bc%e5%8f%98%e8%89%b2%e7%9a%84%e4%bb%a3%e7%a0%81/" rel="bookmark" class="crp_title">点击单[......]</a></li></ul></div><p class='read-more'><a href='http://dallascao.com/cn/change-your-php-and-mysql-timezone/'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p><code>date_default_timezone_set('PRC'); </code></p>
<p>把php环境时区改为中国 (+8)。</p>
<p>用数据的话还要使用</p>
<p><code>mysql_query("SET time_zone = '+8:00'");</code></p>
<p>改变mysql环境时区。</p>
<div id="crp_related"><h3>相关文章：</h3><ul><li><a href="http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">在 .htaccess 中使用正则表达式来定义转向</a></li><li><a href="http://dallascao.com/cn/%e7%82%b9%e5%87%bb%e5%8d%95%e5%85%83%e6%a0%bc%e5%8f%98%e8%89%b2%e7%9a%84%e4%bb%a3%e7%a0%81/" rel="bookmark" class="crp_title">点击单元格变色的代码</a></li><li><a href="http://dallascao.com/cn/plaintext/" rel="bookmark" class="crp_title">PlainText 1.10</a></li><li><a href="http://dallascao.com/cn/google-dictionary-for-word-gd4word/" rel="bookmark" class="crp_title">Google Dictionary for Word</a></li><li><a href="http://dallascao.com/cn/google_cleaning_up/" rel="bookmark" class="crp_title">谷歌将于年底关闭谷歌翻译API</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/cn/change-your-php-and-mysql-timezone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在 .htaccess 中使用正则表达式来定义转向</title>
		<link>http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/</link>
		<comments>http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/#comments</comments>
		<pubDate>Fri, 14 May 2010 17:40:12 +0000</pubDate>
		<dc:creator>Dallas Cao</dc:creator>
				<category><![CDATA[代码狂]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://dallascao.com/cn/?p=390</guid>
		<description><![CDATA[<h2>任务一：</h2>
<p>Apache 提供一个 .htaccess 文件，它可用来进行 URL 转向。例如您想将 http://dallascao.com/cn/ 转到 http://dallascao.com/en/，您可以打开根目录下的 .htaccess 文件，然后输入以下代码：<br />
(建设中&#8230;）<br />
[......]</p><p class='read-more'><a href='http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<h2>任务一：</h2>
<p>Apache 提供一个 .htaccess 文件，它可用来进行 URL 转向。例如您想将 http://dallascao.com/cn/ 转到 http://dallascao.com/en/，您可以打开根目录下的 .htaccess 文件，然后输入以下代码：<br />
(建设中&#8230;）<br />
<code><br />
RewriteRule ^cn\/?$ en\/<br />
</code></p>
<p>简单说，这句话的意思是“把 &#8216;^cn\/?$&#8217; 使用 &#8216;en/&#8217; 代替”。  “^cn\/?$” 就是一个简单的正则表达式：</p>
<p>“^” 匹配字符串的开头<br />
“$” 茶杯字符串结尾<br />
“\” 的意思是它后面的符号”/”代表符号本身，而不是语法符号。<br />
“?” 意思是可以有一个 “/” 或没有 “/”</p>
<p>也就是说  “^cn\/?$” 匹配两个字符串，”cn/” 或 “cn”。访客访问 http://dallascao.com/cn/ 或 http://dallascao.com/cn 时，他们被转向至 http://dallascao.com/en/</p>
<h2>任务二：</h2>
<p>如果您将将 http://dallascao.com/ 转向至 http://dallascao.com/en/，可以使用以下代码：</p>
<p><code><br />
RewriteRule ^$ en\/<br />
</code></p>
<p>“^$” 匹配一个空字符串，即网站根目录。</p>
<h2>任务三：</h2>
<p>把以代码放到 http://gt4t.net/ 根目录，访问 http://gt4t.net/任何字符串/ 的人就会被自动转向至 http://gt4t.net/en/任何字符串/. 例如，访问 http://gt4t.net/downloads/，即被转向至 http://gt4t.net/en/downloads.</p>
<p><code>RewriteRule ^(.*?)\/?$ \/en\/$1 </code></p>
<p>“^(.*?)\/?$” 代表任何以 “/” 或 no “/” 的字符串。表达式的第二部分 “\/en\/$1&#8243;, “$1&#8243; 代表了 “^(.*?)\/?$” 第一个括号中的内容。即以 “/”结束 或没 “/” 的”任何字符串”被成了 “/en/任何字符串/”</p>
<p>此外，如果您想实现隐藏式转向，您可以在开头加入下面这个选项：</p>
<pre lang=“perl” escaped=“true”>Options +FollowSymLinks</pre>
<p>下面就是一个完整的 .htaccess 文件：</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">RewriteEngine on
Options <span style="color: #339933;">+</span>FollowSymLinks
&nbsp;
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_HOST<span style="color: #009900;">&#125;</span> <span style="color: #339933;">^</span>gt4t<span style="color: #339933;">.</span>net$ <span style="color: #009900;">&#91;</span>OR<span style="color: #009900;">&#93;</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_HOST<span style="color: #009900;">&#125;</span> <span style="color: #339933;">^</span>www<span style="color: #339933;">.</span>gt4t<span style="color: #339933;">.</span>net$
RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*?</span><span style="color: #009900;">&#41;</span>\<span style="color: #339933;">/?</span>$ \<span style="color: #339933;">/</span>gt4t_en\<span style="color: #339933;">/</span><span style="color: #0000ff;">$1</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></div></div>

<p>看不懂？ 需要帮忙的就<a href="http://dallascao.com/en/contact-dallas/">联系我吧。</a>。</p>
<div id="crp_related"><h3>相关文章：</h3><ul><li><a href="http://dallascao.com/cn/change-your-php-and-mysql-timezone/" rel="bookmark" class="crp_title">修改php和mysql的时区</a></li><li><a href="http://dallascao.com/cn/use-of-if-fields/" rel="bookmark" class="crp_title">使用 Miscrosoft Word 的 If 域</a></li><li><a href="http://dallascao.com/cn/plaintext/" rel="bookmark" class="crp_title">PlainText 1.10</a></li><li><a href="http://dallascao.com/cn/google-dictionary-for-word-gd4word/" rel="bookmark" class="crp_title">Google Dictionary for Word</a></li><li><a href="http://dallascao.com/cn/%e7%82%b9%e5%87%bb%e5%8d%95%e5%85%83%e6%a0%bc%e5%8f%98%e8%89%b2%e7%9a%84%e4%bb%a3%e7%a0%81/" rel="bookmark" class="crp_title">点击单元格变色的代码</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用 cookies 记住用户上次访问的语言版本（php 代码）</title>
		<link>http://dallascao.com/cn/use-cookies-to-remember/</link>
		<comments>http://dallascao.com/cn/use-cookies-to-remember/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 18:26:32 +0000</pubDate>
		<dc:creator>Dallas Cao</dc:creator>
				<category><![CDATA[代码狂]]></category>
		<category><![CDATA[全球互联网]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[代码]]></category>

		<guid isPermaLink="false">http://dallascao.com/cn/?p=167</guid>
		<description><![CDATA[<p>本网站为双语。http://dallascao.com/cn/ 为中文版本，http://dallscao.com/en/ 为英文版本。访问 http://dallascao.com/ 时，您会被根据您浏览器的默认语言或您上次访问的语言版本重新定向至 /cn/ 或 /en/。我使用了 cookies 来记录您上次访问的语言版本，当您再次访问时，将您定向至该版本。如果您是第一次访问，您打开的将是与您的浏览器默认语言相匹配的语言版本。[......]</p><p class='read-more'><a href='http://dallascao.com/cn/use-cookies-to-remember/'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>本网站为双语。http://dallascao.com/cn/ 为中文版本，http://dallscao.com/en/ 为英文版本。访问 http://dallascao.com/ 时，您会被根据您浏览器的默认语言或您上次访问的语言版本重新定向至 /cn/ 或 /en/。我使用了 cookies 来记录您上次访问的语言版本，当您再次访问时，将您定向至该版本。如果您是第一次访问，您打开的将是与您的浏览器默认语言相匹配的语言版本。<span id="more-167"></span></p>
<p>以下步骤可实现上述目的。首先编辑英文版本的 header.php，将以下代码插入到<html><head> 标签之前：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!--set permanent cookie --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lang&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;en&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;">94608000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;.dallascao.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>以上代码在您的电脑上记录一个变量 “lang” ，赋值 “en”。94608000 是过期时间（3年）。”/” 和”.dallascao.com” 意味着该 cookies 自 dallascao.com 及其下属域名的根目录起效。将代码插入 header.ph 后，当您访问英文版本时，您的访问便会被记住。</p>
<p>同样，您需要在中文版本的 header.php 插入以下代码。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!--set permanent cookie --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lang&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;cn&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;">94608000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;.dallascao.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>也就是说当您访问中文版本时，变量 “lang” 便会被赋值 “cn”。</p>
<p>最后，在要目录创建 index.php，内容为：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$lang</span><span style="color: #339933;">=</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;lang&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lang</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'cn'</span><span style="color: #339933;">:</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:  http://dallascao.com/cn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'en'</span><span style="color: #339933;">:</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:  http://dallascao.com/en'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">#如果没找到 cookies，则读取用户游览器的默认语言。
</span><span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
	<span style="color: #000088;">$lang</span> <span style="color: #339933;">=</span> getDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lang</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'zh-cn'</span> <span style="color: #339933;">:</span> 
               <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:  http://dallascao.com/cn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
               <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> 
       <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> 
	          <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: http://dallascao.com/en'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
              <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> 
		<span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">#########################################################
</span><span style="color: #666666; font-style: italic;"># Copyright ? 2008 Darrin Yeager                        #
</span><span style="color: #666666; font-style: italic;"># http://www.dyeager.org/                               #
</span><span style="color: #666666; font-style: italic;"># Licensed under BSD license.                           #
</span><span style="color: #666666; font-style: italic;">#   http://www.dyeager.org/downloads/license-bsd.php    #
</span><span style="color: #666666; font-style: italic;">#########################################################
</span>
<span style="color: #000000; font-weight: bold;">function</span> getDefaultLanguage<span style="color: #009900;">&#40;</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;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_ACCEPT_LANGUAGE&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span> parseDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_ACCEPT_LANGUAGE&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span>
      <span style="color: #b1b100;">return</span> parseDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> parseDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_accept</span><span style="color: #339933;">,</span> <span style="color: #000088;">$deflang</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en&quot;</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;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_accept</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_accept</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;"># Split possible languages into array
</span>      <span style="color: #000088;">$x</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;">$http_accept</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: #000088;">$x</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: #666666; font-style: italic;">#check for q-value and create associative array. No q-value means 1 by rule
</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;">&quot;/(.*);q=([0-1]{0,1}\.\d{0,4})/i&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$lang</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</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: #b1b100;">else</span>
            <span style="color: #000088;">$lang</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">#return default language (highest q-value)
</span>      <span style="color: #000088;">$qval</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lang</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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;">$value</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$qval</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qval</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$deflang</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deflang</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<div id="crp_related"><h3>相关文章：</h3><ul><li><a href="http://dallascao.com/cn/the-context-sensitive-language-versions-link/" rel="bookmark" class="crp_title">环境感知的语言版本链接</a></li><li><a href="http://dallascao.com/cn/use-of-if-fields/" rel="bookmark" class="crp_title">使用 Miscrosoft Word 的 If 域</a></li><li><a href="http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">在 .htaccess 中使用正则表达式来定义转向</a></li><li><a href="http://dallascao.com/cn/how-to-start-a-public-wave/" rel="bookmark" class="crp_title">如何发布公开 Wave?</a></li><li><a href="http://dallascao.com/cn/plaintext/" rel="bookmark" class="crp_title">PlainText 1.10</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/cn/use-cookies-to-remember/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>环境感知的语言版本链接</title>
		<link>http://dallascao.com/cn/the-context-sensitive-language-versions-link/</link>
		<comments>http://dallascao.com/cn/the-context-sensitive-language-versions-link/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 16:30:04 +0000</pubDate>
		<dc:creator>Dallas Cao</dc:creator>
				<category><![CDATA[代码狂]]></category>
		<category><![CDATA[全球互联网]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[代码]]></category>
		<category><![CDATA[字符串替换]]></category>
		<category><![CDATA[网站管理]]></category>

		<guid isPermaLink="false">http://www.dallascao.com/cn/?p=123</guid>
		<description><![CDATA[大多数双语网站上都有一个固定的指向另一语言版本的链接。不管你在网站一个语言版本的什么位置，点击语言版本链接，打开的总是另一语言版本的首页。而我希望我的访问者打开的是他正在查看的页面的另一语言版本。 我使用 javawscript 的 replace 方法实现了这个功能。[......]<p class='read-more'><a href='http://dallascao.com/cn/the-context-sensitive-language-versions-link/'></a></p>]]></description>
			<content:encoded><![CDATA[<p>大多数双语网站上都有一个固定的指向另一语言版本的链接。不管你在网站一个语言版本的什么位置，点击语言版本链接，打开的总是另一语言版本的首页。而我希望我的访问者打开的是他正在查看的页面的另一语言版本。<br />
<span id="more-123"></span><br />
例如：如果用户访问了：</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://dallascao.com/cn/the-context-sensitive-language-versions-link/</pre></div></div>

<p>点击底部的 “English” 链接，用户打开的是：</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://dallascao.com/en/the-context-sensitive-language-versions-link/</pre></div></div>

<p>也就是这里涉及到是将当前页面 URL 中的 “/cn/” 换成 “/en/”。</p>
<p>以下 php 代码无法给出正确的结果：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url_old</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</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: #000088;">$url_new</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cn&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$url_old</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>   
&lt;a href=<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url_new</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>English&lt;/a&gt;</pre></div></div>

<p>不管您在访问哪个页面， $url_old 返回的总是首页链接 “/en/index.php”。这是因为我使用了“固定链接”功能，URL 被重写了。在服务器端，URL 重写发生于 PHP 代码执行之后，因此 php 字符串 “$url_old” 无法得到后来被重写的实际 URL。以下的 javascript 代码就可以，这是因为java script 是在客户端运行的，可以准确得到客户端使用的 URL:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
var s<span style="color: #339933;">=</span>window.<span style="color: #006633;">location</span>.<span style="color: #006633;">href</span>
r<span style="color: #339933;">=</span>s.<span style="color: #006633;">replace</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span>cn<span style="color: #339933;">/</span>, <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
document.<span style="color: #006633;">write</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a title='English verion of this page' href=&quot;</span><span style="color: #339933;">+</span>r<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&gt;English&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>更多信息：</p>
<p><a target=_blank href="http://www.w3schools.com/jsref/jsref_replace.asp">java replace () 方法</a></p>
<div id="crp_related"><h3>相关文章：</h3><ul><li><a href="http://dallascao.com/cn/use-cookies-to-remember/" rel="bookmark" class="crp_title">使用 cookies 记住用户上次访问的语言版本（php 代码）</a></li><li><a href="http://dallascao.com/cn/use-of-if-fields/" rel="bookmark" class="crp_title">使用 Miscrosoft Word 的 If 域</a></li><li><a href="http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">在 .htaccess 中使用正则表达式来定义转向</a></li><li><a href="http://dallascao.com/cn/chinese-friendfeedcom-receives-favorable-feedbacks-from-fans/" rel="bookmark" class="crp_title">中文版 friendfeed.com 受到爱好者的好评</a></li><li><a href="http://dallascao.com/cn/google-dictionary-for-word-gd4word/" rel="bookmark" class="crp_title">Google Dictionary for Word</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/cn/the-context-sensitive-language-versions-link/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk

Served from: dallascao.com @ 2012-02-05 15:30:11 -->
