Home >php教程 >php手册 >在任意字符集下正常显示网页的方法

在任意字符集下正常显示网页的方法

WBOY
WBOYOriginal
2016-06-21 09:05:39802browse

网页|显示

  • 通常情况下,我们的网页要指定一个编码字符集,如 GB2312、UTF-8、ISO-8859-1 等,这样我们就可以在网页上显示我们指定编码的文字了。但是我们很可能会遇到这种情况,那就是我们可能希望在 ISO-8859-1 编码的网页上显示汉字,或者在 GB2312 编码的网页上显示韩文等。当然一种解决办法就是我们不用 ISO-8859-1 或者 GB2312 编码,而统统都采用 UTF-8 编码,这样我们只要在这种编码下,就可以混合显示各国文字了,这是现在很多网站采用的方法。

    而我这里所说的并非上面这种方法,因为上面这种方法必须要指定字符集为 UTF-8 才可以,一旦用户手工指定为其他字符集,或者可能因为某些原因,那个字符集设置没起作用,而浏览器又没有正确自动识别的话,我们看到的网页还是乱码,尤其是在某些用框架作的网页中,某个框架中的页面如果字符集设置没起作用,在 firefox 中显示乱码而且还没法改变(我是说在不装RightEncode插件的情况下)。

    而我这里介绍的方法即使是把网页指定为 ISO-8859-1 字符集,也能够正确显示汉字、日文等。原理很简单,就是把除了 ISO-8859-1 编码中前128个字符以外的所有其他的编码都用 NCR(Numeric character reference) 来表示。比如“汉字”这两个字,如果我们写成“汉字”这种形式,那么它在任意字符集下都可以正确显示。根据这个原理,我写了下面这个程序,它可以把现有的网页转化为在任意字符集下都能显示的网页。你只需要指定源网页的字符集和源网页,点提交按钮,就可以得到目标网页了。你也可以只转化某些文字,只需要把文字填写到文本框中,并指定这些文字原来的字符集,点提交按钮,就会在页面上面显示编码后的文字了。另外我还编写了 WordPress 的插件,现在我的 Blog 已经可以在任意字符集下都能正确显示了。

    转化程序地址:http://test.coolcode.cn/nochaoscode/

    下载: nochaoscode.php
    <span style="COLOR: #007700"><span style="COLOR: #007700">?</span><span style="COLOR: #0000bb">php</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #007700">function</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">nochaoscode</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$str</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$isemail</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">false</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">$str</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">iconv</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #dd0000">"UTF-16"</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$str</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">for</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$i</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">0</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$i</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700"><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">strlen</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$str</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$i</span><span style="COLOR: #007700">++</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb">$i</span><span style="COLOR: #007700">++</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">        </span><span style="COLOR: #0000bb">$code</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">ord</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$str</span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb">$i</span><span style="COLOR: #007700">}</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">*</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">256</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">+</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">ord</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$str</span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb">$i</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">+</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">1</span><span style="COLOR: #007700">}</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">        </span><span style="COLOR: #007700">if</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$code</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700"><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">128</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">and</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">!</span><span style="COLOR: #0000bb">$isemail</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">            </span><span style="COLOR: #0000bb">$output</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">.=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">chr</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$code</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">        </span><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">else</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">if</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$code</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">!=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">65279</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">            </span><span style="COLOR: #0000bb">$output</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">.=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #dd0000">""</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">$code</span><span style="COLOR: #007700">.</span><span style="COLOR: #dd0000">";"</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">        </span><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">return</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$output</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$_POST</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'encode'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #007700">if</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">==</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #dd0000">''</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #dd0000">'UTF-8'</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #007700">if</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$_FILES</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'file'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'size'</span><span style="COLOR: #007700">]</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">></span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">0</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">nochaoscode</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">file_get_contents</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$_FILES</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'file'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'tmp_name'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">header</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #dd0000">"Content-type: application/octet-stream;"</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">header</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #dd0000">"Content-length: "</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">strlen</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">header</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #dd0000">"Content-Disposition: attachment; filename="</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">$_FILES</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'file'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'name'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">echo</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">else</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #0000bb">header</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #dd0000">"Content-type: text/html; charset=UTF-8"</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">if</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$_POST</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'email'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">)</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">        </span><span style="COLOR: #007700">echo</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">htmlentities</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">nochaoscode</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$_POST</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'email'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">true</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">else</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #007700">{</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">        </span><span style="COLOR: #007700">echo</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">htmlentities</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">nochaoscode</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$encode</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb"> </span><span style="COLOR: #0000bb">$_POST</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'content'</span><span style="COLOR: #007700">]</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">)</span><span style="COLOR: #007700">;</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">    </span><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">?></span> </span></span></span>

    encode:

    file:




    encode:

    content:




    encode:

    email:



    <font face="新宋体"><span style="COLOR: #007700"><span style="COLOR: #007700">?</span><span style="COLOR: #0000bb">php</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #007700">}</span><span style="COLOR: #0000bb"> </span><br><span style="COLOR: #0000bb">?></span> </span></font>


  • Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn