PHP Output Control Function in simplified and Traditional Chinese conversion application Overview: 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 ";
?>
<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
}
}
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.