Application of PHP output control function in simplified and traditional Chinese conversion

Source: Internet
Author: User
Summary: this article briefly introduces the PHP output control function and provides specific ideas for its application in simplified and traditional Chinese conversion. Example 1: Introduction to the PHP output control function as one of the popular script languages today, it 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. it can be used for the following 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 data blocks similar to echo (), print (), and PHP code.
Example 1. control output
Test. php
Function test ($ str ){
Return str_replace ("php2000", "y10k", $ str );
}
Ob_start ("test ");
Echo "hello php2000 ";
Ob_end_flush ();
?>
This program should output
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. There are a lot of articles related to it. here we will not talk about it more, just give its implementation code
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 ";
?>

If ($ php2000_big5 = 1) echo "GB ";
Else echo "Big5 ";
?>

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
}
}
Ob_start ('translate _ gb2big5 ');
}
?>
For more information about how to use the shared memory, see.
Conclusion 4
PHP, as a scripting language that exposes source code, 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.