search
HomeBackend DevelopmentPHP TutorialPHP中字体应用释疑_PHP

综述:有许多朋友对网站提供繁、简两种版本感到很困惑,是怎么实现的呢?这也是时下众多PHP书籍中被漏掉的一个很重要的知识点。笔者搜集整理并根据自己的开发经验将一些重点与疑点罗列出来与大家共享!

  如何应用繁体中文转换为简体中文的PHP函数

  我们定义一个big5togb的函数来实现这个转换:

function big5togb($code)
{
//参数$code是big5码的字符串
include "data_big5.php"; //包含big5数据的文件
$output="";
$length=strlen($code); //取得字符串长度
$code=strtok($code,"");
$idx=0;
while ($idx < $length)
{
$tmpStr=$code[$idx].$code[$idx 1];

if (isbig5($tmpStr)) //判断是否big5码
{
……//如果是big5码则进行转换后输出
}
else
{
$output.= $code[$idx]; //如果不是big5码则直接输出
}
$idx ;
}
return ($output);
}

  如何应用简体中文转换为繁体中文的PHP函数?

  如何用PHP将简体中文转换为繁体中文?

  我们定义一个big5togb的函数来实现这个转换:

function gbtobig5($code)
{
include "data_gb.php"; //包含有gb码的数据文件
$output="";
$length=strlen($code);
$code=strtok($code,"");
$idx=0;
while ($idx < $length)
{
$tmpStr=$code[$idx].$code[$idx 1];

if (isgb($tmpStr)) //判断是否gb码
{
……//如果是gb码转换后输出
}
else
{
$output.= $code[$idx]; //不是gb码则直接输出
}
$idx ;
}
return ($output);
}
  在简繁体转换中怎样应用PHP输出控制功能?

  PHP输出控制功能是怎样一回事?

  PHP的输出信息控制函数可以让你控制你的脚本输出的内容,可以用于许多不同的情况,特别是在你的脚本已经输出信息后需要发送文件头的情况以及需要对输出信息进行编辑处理的地方。输出控制函数不对使用header()或setcookie()发送的文件头信息产生影响,只对那些类似于echo()、print() 和 PHP 代码的数据块有作用。

  例 1. 控制输出

test.php
<?
function test($str){
return str_replace("world","php",$str);
}
ob_start("test");
echo "hello world";
ob_end_flush();
?>

  这个程序在没有输出信息控制的情况下应该输出为
hello world

  但通过指定了输出控制函数后,输出变为
hello php

  在上面的例子中,使用echo()的输出内容将会保存在输出缓冲区中,直到调用了ob_end_flush()或者脚本运行终止,然后输出信息由自定义的处理函数进行处理(替换里面的字符串)并返回结果。

  相关函数说明:

  void ob_start([string output_callback])- 打开输出缓冲区

  所有的输出信息不在直接发送到浏览器,而是保存在输出缓冲区里面,可选得回调函数用于处理输出结果信息。

  void ob_end_flush(void) - 结束(发送)输出缓冲区的内容,关闭输出缓冲区

  简繁体转换如何实现?

  简繁转换一般通过对照表的形式实现,我们这里只给出其实现代码其中:

<?php
function gb2big5($str) {
global $_gb_big5_;
$leng = strlen($str)-1;
for($i = 0; $i<$leng; $i ){
$h = ord($str[$i]);
if($h>=160){
$l = ord($str[$i 1]);
$gb=($h==161 && $l==64)?" " : substr($_gb_big5_, ($h-160)*510 ($l-1)*2, 2);
$str[$i] = $gb[0];
$str[$i 1] = $gb[1];
$i ;
}
}
return $str;
}
?>


  $gb_big5_ 保存着big5 的字库对照表

  $str 为要转化的字符串

  输出控制函数在简繁体转化中如何应用?

  目前的大多数网站的简繁体页面转换都是通过各自单独的页面实现的,这样导致在修改简体页面的时候还需要再次修改繁体的页面,不能做到自动同步。而我们提供的这个方法,可以实现同一个页面自动的变换简繁体显示。其实现方法是:

  1 建立简繁体标志,用于指示当前显示的简繁体状态,同时对简繁体状态进行切换gb_big5.php

<?
session_start(); // 打开session 功能,用于在各个页面之间自动传递标志
if(!session_is_registered("big5")){ // 检查简繁体标志的注册状态
session_register("big5"); // 注册简繁体标志,简体=0;繁体=1
$ big5=0; // 默认为简体
}
$big5 = ($big5 1)%2; // 切换简繁体状态
header("location:".getenv("HTTP_REFERER")); // 返回其调用页面
?>

  2 对页面输出信息进行控制,每个页面都调用这段程序,用于简繁体转换require.php(应包括前面第二部分的转换代码,这里略)

<?
Session_start();
function translate_gb2big5($str) {
$str = gb2big5($str); // 转化为 big5
$str = str_replace('charset=gb2312', 'charset=big5', $str); // 替换字符类型
header('Content-Type: text/html; charset=big5'); // 繁体文件头
return $str;
}
if(session_is_registered("56big5") && ($big5==1)){ // 判断标志
$fp = fopen('big5.table', 'r'); // big5的字库表
$_gb_big5_ = fread($fp, filesize('big5.table')); // 读出数据
fclose($fp);
ob_start('translate_gb2big5'); // 启动输出信息控制
}
?>

  3 使用方法,这里给出一个最简单的例子,放在和 require.php 同一个目录里面test.php

<?
require("require.php");
echo "你好,欢迎光临!";
?>
<a href=gb_big5.php>
<?
if($big5==1)echo "GB";
else echo "Big5";
?>
</a>

  第一次运行结果为默认简体如下
  你好,欢迎光临!
  点击 Big5 连接显示繁体如下
  你好,歡迎光臨! GB
  点击 GB 将返回简体页面

  由于使用了session 保存了简繁体标志,这样其他任何使用了 require.php 的页面都会自动按照当前的标志显示相应的页面。

  4 big5 字库的保存的改进方法

  可以使用 session 来保存 big5 字库,但使用后速度会明显减慢,主要因为 session 也是通过文件形式实现,所以不会对性能产生提高,而且因为session 不会根据简繁体标志自动判断装载与否,所以造成在简体下也装载了 big5 的字库,所以造成速度减慢。

  如果服务器为 linux 可以考虑使用共享内存来保存 big5 字库信息。其更改的代码为require.php 的判断部分:

<?
if(session_is_registered("big5") && ($big5==1))
{
// 修改成使用共享内存
// 判断是否已经创建,打开50000字节的 0xff3 段的共享内存
$shm_id = @shmop_open(0xff3, "a", 0644, 50000);
if($shm_id) {
$_gb_big5_ = shmop_read($shm_id, 0,shmop_size($shm_id)); // 读出big5 数据
}
else{
// 创建 50000 字节的系统标识为 0xff3 的共享内存块
$shm_id = @shmop_open(0xff3, "c", 0644, 50000);

// 读出数据
$fp = fopen('big5.table', 'r');
$_gb_big5_ = fread($fp, filesize('big5.table'));
fclose($fp);

if($shm_id){
$shm_bytes_written = shmop_write($shm_id, $_gb_big5_,0); // 写入 big5 数据
}
}
ob_start('translate_gb2big5');
}
?>

  如何分别全角和半角以避免乱码?

  我们可以写这样一个函数来实现:

function ChgTitle($title)
{
$length = 46; //我们允许字符串显示的最大长度
if (strlen($title)>$length) {
$temp = 0;
for($i=0; $i<$length; $i )
if (ord($title[$i]) > 128) $temp ;
if ($temp%2 == 0)
$title = substr($title,0,$length)."...";
else
$title = substr($title,0,$length 1)."...";
}
return $title;
}

  这个函数原理就是截断一个字符,看看其ascII码是不是大于128,如果是,说明截断的是一个全角汉字,那么就退后一个截断。用$length控制长度

  备注:循环判断字符串里面的 >128 的字符个数,如果半角字符为偶数,则表示位置刚好为整个汉字,如果为奇数,则为半个汉字,需要取下一个字符

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool