PHP中字体应用释疑_PHP
如何应用繁体中文转换为简体中文的PHP函数
我们定义一个big5togb的函数来实现这个转换:
{
//参数$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的函数来实现这个转换:
{
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的输出信息控制函数可以让你控制你的脚本输出的内容,可以用于许多不同的情况,特别是在你的脚本已经输出信息后需要发送文件头的情况以及需要对输出信息进行编辑处理的地方。输出控制函数不对使用header()或setcookie()发送的文件头信息产生影响,只对那些类似于echo()、print() 和 PHP 代码的数据块有作用。
例 1. 控制输出
<?
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) - 结束(发送)输出缓冲区的内容,关闭输出缓冲区
简繁体转换如何实现?
简繁转换一般通过对照表的形式实现,我们这里只给出其实现代码其中:
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');
}
?>
如何分别全角和半角以避免乱码?
我们可以写这样一个函数来实现:
{
$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 的字符个数,如果半角字符为偶数,则表示位置刚好为整个汉字,如果为奇数,则为半个汉字,需要取下一个字符

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. Open the phone settings and click [Display]. 2. Click [Font]. 3. Select the font you like or click [More Fonts] to download the application.

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

How to change the font in mobile QQ browser? Many people like to use QQ browser on mobile phones. This browser can not only browse web pages quickly, but also process various types of file data. Moreover, this browser also has personalized settings. You can set the font of the browser according to your own preferences. , many novice users still don’t know how to change the fonts of this browser. This article will give you an overview of the steps to change fonts in mobile QQ browser, hoping to help you solve the problem. List of steps to change fonts in mobile QQ Browser 1. Turn on the phone, and then click to open "QQ Browser" (as shown in the picture). 2. On the "My" page of QQ Browser, click the "Settings" logo in the upper right corner (as shown in the picture). 3. In the settings, click to enter "Font Settings" (as shown in the figure)

The Linuxtee command is a very useful command line tool that can write output to a file or send output to another command without affecting existing output. In this article, we will explore in depth the various application scenarios of the Linuxtee command, from entry to proficiency. 1. Basic usage First, let’s take a look at the basic usage of the tee command. The syntax of tee command is as follows: tee[OPTION]...[FILE]...This command will read data from standard input and save the data to

1. Through settings, enter and modify the font style of the mobile phone system. 2. Open the phone settings and enter [Accessibility]. 3. Click [Subtitles and Closed Subtitles]. 4. Click [Style] to make modifications.

How to solve the problem of blurry fonts in Win10 system on Mechanic i9 Sugon 16? Win10 system font is blurry, how to solve it? (Take my I9 Sugon 16 as an example) When using computers, many users may encounter fonts that are blurred and difficult to read. To solve this problem, we provide you the following tutorial. These methods come from daily interactions among Mechanic fans and apply to all makes and models of computers. Solution 1: Adjust the display resolution. Many computer friends have new computers or after reinstalling the system, it is caused by the graphics card driver. So how do we know if it is a problem with the graphics card driver? Step 1: Right-click on the desktop and select Graphics Card Settings in the pop-up options bar. (Sorry, Youwei is using a virtual machine. I tried simulating the fault phenomenon but it didn’t work.

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword
