SoFunction
Updated on 2025-04-08

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

Summary: This article briefly introduces the output control function of PHP and gives specific ideas and examples for its application in simplified and traditional Chinese transformation.

1. Introduction to PHP output control function
As one of the popular scripting languages ​​today, PHP has the advantages of easy writing, fast execution, and good expansion. PHP's output information control function allows you to control the content output of your script. It can be used in many different situations, especially when your script has outputted information and needs to send file headers and where the output information needs to be edited and processed. The output control function does not affect the file header information sent using header() or setcookie(), but only works for data blocks similar to echo(), print(), and PHP code.
Example 1. Control output

<?
function test($str){
return str_replace("php2000","y10k",$str);
}
ob_start("test");
echo "hello php2000";
ob_end_flush();
?>
This program should output as
hello php2000
However, after specifying the output control function, the output becomes
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 run is terminated. Then the output information is processed by a custom processing function (replace the string inside) and the result is returned.

Related function description
ob_start([string output_callback]) - Open the output buffer
All output information is not sent directly to the browser, but is stored in the output buffer. The callback function can be selected to process the output result information.
ob_end_flush - end (send) the contents of the output buffer and close the output buffer

2. Implementation of Simplified Traditional Chinese Conversion
Generally, it is implemented in the form of a comparison table. There are many related articles, so I won't talk about it here, only the implementation code is given.
<?
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;
}
?>
in:
$gb_big5_ Saves the font library comparison table of big5
$str is the string to be converted
3. Application of output control function in simplified and traditional Chinese transformation
The conversion of simplified and traditional Chinese pages of most websites currently is achieved through their own separate pages, which leads to the need to modify the traditional Chinese pages again when modifying the simplified pages, and automatic synchronization cannot be achieved. The method we provide can automatically transform the Simplified Traditional Chinese display on the same page. The implementation method is:
1 Create a simplified traditional Chinese flag to indicate the currently displayed simplified traditional Chinese state, and switch the simplified traditional Chinese state at the same time
php2000_gb_big5.php
<?
session_start(); // Turn on the session function to automatically pass flags between pages
if(!session_is_registered("php2000_big5")){ // Check the registration status of the Simplified Traditional Chinese flag
session_register("php2000_big5"); // Register the Simplified Traditional Chinese flag, Simplified Traditional Chinese = 0; Traditional Chinese = 1
$php2000_big5=0; // Default is Simplified
}
$php2000_big5 = ($php2000_big5+1)%2; // Switch to the simplified and traditional Chinese state
header("location:".getenv("HTTP_REFERER")); // Return to its calling page
?>
2 Control the page output information, and each page calls this program for simplified and traditional Chinese conversion
(The conversion code in the second part of the previous should be included, omitted here)
<?
Session_start();
function translate_gb2big5($str) {
$str = gb2big5($str); // Convert to big5
$str = str_replace('charset=gb2312', 'charset=big5', $str); // Replace character type
header('Content-Type: text/html; charset=big5'); // Traditional file header
return $str;
}
if(session_is_registered("php2000_big5") && ($php2000_big5==1)){ // Judgment flag
$fp = fopen('', 'r'); // big5's font library table
$_gb_big5_ = fread($fp, filesize('')); // Read out the data
fclose($fp);
ob_start('translate_gb2big5'); // Start output information control
}
?>
3. Here is the simplest example, which is placed in the same directory as

<?
require("");
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 run result is the default simplified version as follows
Hello everyone, this is PHP Century Network Big5
Click Big5 to connect to display traditional Chinese as follows
Hello everyone, here is PHP Century Network GB
Clicking GB will return to the simplified page
Since session is used to save the simplified and traditional Chinese logo, any other page that is used will automatically display the corresponding page according to the current logo. For more examples, please see my website http://.
4 Big5 font library improvement method
I have considered using session to save the big5 font library, but after using it, I found that the speed was significantly slowed down. It is mainly because session is also implemented in file form, so it will not improve performance. Moreover, because session will not automatically determine whether it is loaded based on the simplified traditional Chinese flag, the big5 font library is also loaded in simplified Chinese, which causes the speed to slow down.
Since the server I use is Linux, I consider using shared memory (Windows does not support shared memory) to save big5 font library information. The code that changes is the judgment part of:
<?
if(session_is_registered("php2000_big5") && ($php2000_big5==1))
{
// Modify to use shared memory
// Determine whether it has been created and open the shared memory of 0xff3 segment of 50,000 bytes
$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 system identifiable 0xff3
$shm_id = @shmop_open(0xff3, "c", 0644, 50000);

// Read out the data
$fp = fopen('', 'r');
$_gb_big5_ = fread($fp, filesize(''));
fclose($fp);

if($shm_id){
$shm_bytes_writen = shmop_write($shm_id, $_gb_big5_,0); // Write big5 data
}
}
ob_start('translate_gb2big5');
}
?>
For how to use shared memory, please refer to the relevant information.
Four Conclusion
As a scripting language that exposes source code, PHP has very good expansion. This article is just an exploration of one of its functions and realizes a relatively perfect automatic simplified and traditional Chinese conversion function on the same page. I hope that all PHP-loving friends can get inspiration from it and make better works.