The font application in PHP is an explanation

Source: Internet
Author: User
Tags define header implement ord php books php code strlen strtok
Summary: There are many friends of the site to provide complex, Jane two versions of the confusion, how to achieve it? This is one of the most important points of knowledge that is missing from many PHP books nowadays. The author collects and collates and according to own development experience will some key points and the doubtful list to share with everybody!

How to apply the traditional Chinese to Simplified Chinese php functions?

We define a BIG5TOGB function to implement this transformation:

function Big5togb ($code)
{
Parameter $code is a string of BIG5 code
Include "data_big5.php"; Files that contain BIG5 data
$output = "";
$length =strlen ($code); Get string length
$code =strtok ($code, "");
$idx = 0;
while ($idx $length)
{
$TMPSTR = $code [$idx]. $code [$idx +1];

if (Isbig5 ($TMPSTR))//judge whether Big5 code
{
...//If it is a BIG5 code then the conversion output
}
Else
{
$output. = $code [$idx]; Direct output if not BIG5 code
}
$idx + +;
}
return ($output);
}

How do I convert Simplified Chinese to Traditional Chinese PHP functions?

How to use PHP to convert Simplified Chinese to traditional Chinese?

We define a BIG5TOGB function to implement this transformation:

function Gbtobig5 ($code)
{
Include "data_gb.php"; Data files that contain GB code
$output = "";
$length =strlen ($code);
$code =strtok ($code, "");
$idx = 0;
while ($idx $length)
{
$TMPSTR = $code [$idx]. $code [$idx +1];

if (ISGB ($TMPSTR))//To determine whether GB code
{
...//If it is GB code conversion after the output
}
Else
{
$output. = $code [$idx]; Direct output if not GB code
}
$idx + +;
}
return ($output);
}

How to apply the PHP output control function in the simplified traditional conversion?

What is the output control function of PHP?

The output information control function of PHP allows you to control the content of your script output and can be used in a number of different situations, especially where your script has to send a header after it has been exported, and where the output information needs to be edited. The output control function does not affect file header information sent using header () or Setcookie (), only for blocks of data that resemble the Echo (), print (), and PHP code.

Example 1. Control output

test.php
?
function test ($STR) {
Return Str_replace ("The World", "PHP", $str);
}
Ob_start ("test");
echo "Hello World";
Ob_end_flush ();
? >

This program should output as if no output information is controlled.
Hello World

But by specifying the output control function, the output becomes
Hello PHP

In the above example, the output with Echo () will be saved in the output buffer until the Ob_end_flush () is invoked or the script run terminates, and the output information is processed by the custom handler (replacing the inside string) and returns the result.

Related function Description:

void Ob_start ([string output_callback])-Open output buffer

All output information is not sent directly to the browser, but is stored in the output buffer, which is used to process output information.

void Ob_end_flush (void)-end (send) output buffer content, turn off output buffer

How does a simplified traditional transformation be implemented?

Simple transformation is generally achieved through the form of a table, we only give its implementation code here:

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_ Keep the big5 of the font

$str for the string to be converted

4 improved method for saving Big5 font

You can use the session to save the Big5 font, but the speed is significantly slower after use, mainly because the session is also implemented in a file format , so will not improve performance, and because the session will not be based on simple traditional signs automatically determine whether the load or not, so that in the simplified also loaded the big5 of the font, so caused by slow.

If the server is Linux, you can consider using shared memory to save the Big5 font information. The code for the change is the require.php part of the decision:

?
if (session_is_registered ("Big5") && ($big 5==1))
{
Modified to use shared memory
Determine if you have created, open 50000-byte shared memory for the 0XFF3 segment
$shm _id = @shmop_open (0XFF3, "a", 0644, 50000);
if ($shm _id) {
$_gb_big5_ = Shmop_read ($shm _id, 0,shmop_size ($shm _id)); Read out BIG5 data
}
else{
Create a 50000-byte shared memory block with a system identified as 0XFF3
$shm _id = @shmop_open (0XFF3, "C", 0644, 50000);

Read the data
$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); Write Big5 Data
}
}
Ob_start (' Translate_gb2big5 ');
}
? >

   How to separate full-width and half-width to avoid garbled?

We can write such a function to achieve:

function Chgtitle ($title)
{
$length = 46; We allow the maximum length of string display
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;
}

The principle of this function is to truncate a character to see if its ASCII code is greater than 128, if it is, the truncation is a full-width Chinese character, then back up a truncation. Control length with $length

Note: Loop to determine the number of >128 characters inside the string, if the Half-width word identifier even, it means that the position is just the entire Chinese character, if it is odd, then half Chinese characters, need to take off a character



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.