PHP font application explanation

Source: Internet
Author: User
Summary: many friends are confused about the traditional and simplified versions of the website. how can this problem be achieved? This is also a very important point missed in many PHP books. I collected and sorted out some key points and questions based on my development experience and shared them with you! How can I use PHP functions that convert traditional Chinese to simplified Chinese? We define a big5to Summary:Many of my friends are confused about the traditional and simplified versions of the website. how can this problem be achieved? This is also a very important point missed in many PHP books. I collected and sorted out some key points and questions based on my development experience and shared them with you!

   How can I use PHP functions that convert traditional Chinese to simplified Chinese?

We define a big5togb function to implement this conversion:

Function big5togb ($ code)
{
// The parameter $ code is a string of the big5 code.
Include "data_big5.php"; // file containing big5 data
$ Output = "";
$ Length = strlen ($ code); // Obtain the string length.
$ Code = strtok ($ code ,"");
$ Idx = 0;
While ($ idx <$ length)
{
$ TmpStr = $ code [$ idx]. $ code [$ idx 1];

If (isbig5 ($ tmpStr) // determines if big5 code is used
{
...... // If it is a big5 code, convert it and output it
}
Else
{
$ Output. = $ code [$ idx]; // output directly if it is not a big5 code
}
$ Idx;
}
Return ($ output );
}
   How can I use PHP functions that convert simplified Chinese to traditional Chinese?

How can I use PHP to convert simplified Chinese to traditional Chinese?

We define a big5togb function to implement this conversion:

Function gbtobig5 ($ code)
{
Include "data_gb.php"; // a data file containing a gb code
$ Output = "";
$ Length = strlen ($ code );
$ Code = strtok ($ code ,"");
$ Idx = 0;
While ($ idx <$ length)
{
$ TmpStr = $ code [$ idx]. $ code [$ idx 1];

If (isgb ($ tmpStr) // Determine whether the code is gb.
{
...... // Output after gb code conversion
}
Else
{
$ Output. = $ code [$ idx]; // output directly if it is not a gb code
}
$ Idx;
}
Return ($ output );
} In simplified and traditional Chinese conversion, how does one apply the PHP output control function?

What is the PHP output control function?

The PHP output information control function allows you to control the output content of your script, which can be used in many different situations, especially when your script has output information, you need to send the file header and edit the output information. The output control function does not affect the header information sent using header () or setcookie (). it only applies to data blocks similar to echo (), print (), and PHP code.

Example 1. control output

Test. php
<?
Function test ($ str ){
Return str_replace ("world", "php", $ str );
}
Ob_start ("test ");
Echo "hello world ";
Ob_end_flush ();
?>
This program should output
Hello world

However, after an output control function is specified, the output is changed
Hello php

In the above example, the output content using echo () will be saved in the output buffer until ob_end_flush () is called or the script stops running, then, the output information is processed by the custom processing function (replacing the strings in it) and the result is returned.

Function description:

Void ob_start ([string output_callback])-open the output buffer

All output information is not directly sent to the browser, but saved in the output buffer. an optional callback function is used to process output result information.

Void ob_end_flush (void)-end (SEND) output buffer content, close the output buffer

   How to implement simplified and traditional Chinese conversion?

Simple and traditional conversions are generally implemented in the form of a comparison table. here we only provide its implementation code:

<? 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 _ saves the big5 font table

$ Str is the string to be converted

  How can output control functions be applied in simplified and traditional Chinese conversion?

Currently, most websites use their own simple and traditional pages for conversion. as a result, you need to modify the traditional pages again when modifying the simplified pages. We provide this method to automatically change the traditional display of the same page. The implementation method is as follows:

1. create a simplified and traditional Chinese flag to indicate the current simplified and traditional status. at the same time, switch the simplified and traditional status to gb_big5.php.

<?
Session_start (); // enable the session function to automatically pass the flag between pages.
If (! Session_is_registered ("big5") {// check the registration status of the simplified and traditional Chinese logo
Session_register ("big5"); // sign up for simplified and traditional Chinese characters, simplified = 0; traditional = 1
$ Big5 = 0; // The default value is simplified.
}
$ Big5 = ($ big5 1) % 2; // switch between Simplified and Traditional Chinese
Header ("location:". getenv ("HTTP_REFERER"); // return to its call page
?>
2. Control page output information. each page calls this program for simplified and traditional conversion of require. php (the conversion code of the second part should be included here)

<?
Session_start ();
Function translate_gb2big5 ($ str ){
$ Str = gb2big5 ($ str); // Convert to big5
$ Str = str_replace ('charset = gb2312 ', 'charset = big5', $ str); // replace the character type
Header ('content-Type: text/html; charset = big5'); // Traditional file header
Return $ str;
}
If (session_is_registered ("56big5") & ($ big5 = 1) {// judge the flag
$ Fp = fopen ('big5. table', 'r'); // big5 font table
$ _ Gb_big5 _ = fread ($ fp, filesize ('big5. table'); // read data
Fclose ($ fp );
Ob_start ('translate _ gb2big5 '); // start output information control
}
?>
3. here is a simple example, which is put in the same directory as require. php.

<?
Require ("require. php ");
Echo "Hello, welcome! ";
?>
<A href = gb_big5.php>
<?
If ($ big5 = 1) echo "GB ";
Else echo "Big5 ";
?>
</A>
The first running result is simplified as follows by default.
Hello, welcome!
Click Big5 connection to display the following traditional Chinese characters:
Hello, welcome! GB
Click GB to return to the simplified page

Since the session is used to save the simplified and traditional logo, any other page that uses require. php will automatically display the corresponding page according to the current logo.

  4 Improvement Methods for saving big5 font

You can use the session to save the big5 font, but the speed will be significantly slowed down after use, mainly because the session is also implemented in the form of files, so it will not improve the performance, in addition, because the session does not automatically judge whether to load according to the simplified and traditional Chinese characters, the big5 font is also loaded in simplified Chinese characters, resulting in a slow speed.

If the server is linux, you can use the shared memory to save the big5 font information. The changed code is the judgment part of require. php:

<?
If (session_is_registered ("big5") & ($ big5 = 1 ))
{
// Modify to use shared memory
// Determine whether the memory has been created and enable the 50000-byte 0xff3 shared memory
$ Shm_id = @ shmop_open (0xff3, "a", 0644,500 00 );
If ($ shm_id ){
$ _ Gb_big5 _ = shmop_read ($ shm_id, 0, shmop_size ($ shm_id); // read big5 data
}
Else {
// Create a 50000-byte shared memory block identified as 0xff3
$ Shm_id = @ shmop_open (0xff3, "c", 0644,500 00 );

// Read 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 can we avoid garbled characters by means of fullwidth and halfwidth?

We can write such a function to implement:

Function ChgTitle ($ title)
{
$ Length = 46; // The maximum length allowed for 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 truncation a character to see if its ascII code is greater than 128. If yes, it indicates that the truncation is a full-angle Chinese character, then a truncation is taken back. Use $ length to control the length

Note: cyclically judge the number of> 128 characters in a string. if The halfwidth character is an even number, it indicates that the position is exactly the whole Chinese character. if it is an odd number, it is half a Chinese character, the next character is required.

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.