Application of PHP Output Control Function in simplified and Traditional Chinese Conversion

Source: Internet
Author: User
Tags comparison table

Application of PHP Output Control Function in simplified and Traditional Chinese Conversion

Summary: This article briefly introduces the PHP output control function and provides specific ideas and examples for its application in simplified and Traditional Chinese conversion.

I. Introduction to PhP Output Control
As one of the most popular scripting languages, PHP has the advantages of easy writing, fast execution, and good scalability. 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 files similar to echo (), print (), and PHPCode.
Example 1. control output
Test. php
<?
Function Test ($ Str ){
Return str_replace ("php2000", "y10k", $ Str );
}
Ob_start ("test ");
Echo "Hello php2000 ";
Ob_end_flush ();
?>
ThisProgramIf no output information is controlled, the output is
Hello php2000
However, after an output control function is specified, the output is changed
Hello y10k
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
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.
Ob_end_flush-end (send) output buffer content, disable the output buffer

Implementation of simplified and Traditional Chinese Conversion
It is generally implemented in the form of a comparison table. Article A lot of code is provided 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;
}
?>
Where:
$ Gb_big5 _ saves the big5 font table
$ STR is the string to be converted
Application of Three output control functions 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.
Php2000_gb_big5.php
<?
Session_start (); // enable the session function to automatically pass the flag between pages.
If (! Session_is_registered ("php2000_big5") {// check the registration status of the simplified Chinese logo
Session_register ("php2000_big5"); // register the simplified and Traditional Chinese logo, which is simplified = 0; traditional = 1
$ Php2000_big5 = 0; // The default value is simplified.
}
$ Php2000_big5 = ($ php2000_big5 + 1) % 2; // switch the simplified and Traditional Chinese Status
Header ("Location:". getenv ("http_referer"); // return to its call page
?>
2. Control the page output information. Each page calls this program for simplified and traditional conversion.
Require. php (the conversion code of the second part should be included, which is omitted 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 ("php2000_big5") & ($ php2000_big5 = 1) {// identify a 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.
Test. php
<?
Require ("require. php ");
Echo "Hello everyone, this is Php century network ";
?>
<A href = php2000_gb_big5.php>
<?
If ($ php2000_big5 = 1) echo "GB ";
Else echo "big5 ";
?>
</A>
The first running result is simplified as follows by default.
Hello everyone, this is Php century network big5
Click big5 connection to display the following traditional Chinese characters:
Hello everyone, this is the PHP world site 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. For more instances, see my website http://www.php2000.com.
4 improvement methods for saving big5 font
We used to consider using sessions to save the big5 font library. However, we found that the speed slowed down significantly because sessions are also implemented in the form of files, so the performance will not be improved, 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.
Because the server I use is Linux, I consider using shared memory (Windows does not support shared memory) to save big5 font information. The changed code is the judgment part of require. php:
<?
If (session_is_registered ("php2000_big5") & ($ php2000_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
}< BR >}< br> ob_start ('translate _ gb2big5 ');
}< BR >?>
for details about how to use the shared memory, see related documents.
conclusion
As a script language that discloses Source Code , PHP has excellent scalability. This article only explores one of its functional application methods, and implements a perfect automatic simplified and traditional conversion function for the same page. I hope that the majority of PHP-loving friends will be inspired to make better works.

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.