Home  >  Article  >  Backend Development  >  Application of PHP output control function in Simplified and Traditional Chinese conversion_PHP tutorial

Application of PHP output control function in Simplified and Traditional Chinese conversion_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:10:00904browse


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 conversion

An introduction to PHP output control function
PHP is a popular One of the scripting languages, it has the advantages of easy writing, fast execution speed, and good scalability. PHP's output information control function allows you to control the content output by your script and can be used in many different situations, especially when you need to send a file header after your script has output information and when you need to edit the output information. place. The output control function does not affect the file header information sent using header() or setcookie(), only those 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 as
hello php2000
But 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 execution terminates, the output information is processed by the custom processing function (replacing 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 saved in the output buffer, optional The callback function is used to process the output result information.
ob_end_flush - End (send) the contents of the output buffer, close the output buffer

Implementation of Simplified and Traditional Chinese conversion
Generally implemented in the form of a comparison table, there are many related articles, here are Not much to say, just give the 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;
}
?>
Among them:
$gb_big5_ holds the font comparison table of big5
$str For the string to be converted
Application of the three output control functions in Simplified and Traditional Chinese conversion
Currently, the conversion of Simplified and Traditional Chinese pages on most websites is implemented through separate pages, which results in the modification of the Simplified Chinese page. Sometimes the Traditional Chinese page needs to be modified again, and automatic synchronization cannot be achieved. The method we provide can automatically convert the same page into simplified and traditional Chinese display. The implementation method is:
1 Establish a Simplified and Traditional Chinese flag to indicate the currently displayed Simplified and Traditional Chinese status, and switch the Simplified and Traditional Chinese status at the same time
php2000_gb_big5.php
session_start(); // Turn on the session function, which is used to automatically transfer logos between pages
if(!session_is_registered("php2000_big5")){ // Check the registration status of Simplified and Traditional Chinese logos
session_register("php2000_big5"); / / Register Simplified and Traditional Chinese flags, Simplified Chinese=0; Traditional Chinese=1
$php2000_big5=0; // Default is Simplified Chinese
}
$php2000_big5 = ($php2000_big5+1)%2; // Switch to Simplified and Traditional Chinese Status
header("location:".getenv("HTTP_REFERER")); // Return to its calling page
?>
2 controls the page output information, and each page calls this program , used for Simplified and Traditional Chinese conversion
require.php (should include the previous second part of the conversion code, 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 Chinese file header
return $str;
}
if(session_is_registered("php2000_big5") && ($php2000_big5==1) ){ // Judgment flag
$fp = fopen('big5.table', 'r'); // Big5's font table
$_gb_big5_ = fread($fp, filesize('big5.table') ); // Read data
fclose($fp);
ob_start('translate_gb2big5'); // Start output information control
}
?>
3 How to use, here Give the simplest example, place it 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 run result is the default simplified Chinese as follows
Hello everyone, this is PHP Century Network Big5
Click Big5 connection Traditional Chinese is displayed as follows
Hello everyone, this is PHP Century Network GB
Click GB to return to the simplified Chinese page
Since the session is used to save the simplified and traditional Chinese flag, any other pages that use require.php will automatically follow the The current logo displays the corresponding page. For more examples, please see my website http://www.php2000.com.
4 Improved methods for saving big5 fonts
I once considered using session to save big5 fonts, but after using it, I found that the speed slowed down significantly, mainly because session is also implemented in the form of files, so it will not improve performance, and Because the session does not automatically determine whether to load based on the Simplified and Traditional Chinese flags, the big5 font is also loaded under Simplified Chinese, which causes a slowdown.
Since the server I am using is Linux, I considered using shared memory (Windows does not support shared memory) to save the 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 it has been created, open the 50000-byte shared memory of segment 0xff3
$shm_id = @shmop_open(0xff3, "a", 0644, 50000);
if($shm_id) {
$_gb_big5_ = shmop_read($shm_id, 0,shmop_size($shm_id)); // Read big5 data
}
else{
// Create a 50000-byte system identifier as 0xff3 Shared memory block
$shm_id = @shmop_open(0xff3, "c", 0644, 50000);

// 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 how to use shared memory, please refer to the relevant information.
Four Conclusion
As an open source scripting language, PHP has very good scalability. This article only discusses an application method of one of its functions, and implements a relatively perfect automatic Simplified and Traditional Chinese conversion function on the same page. I hope that friends who love PHP can get inspiration from it and make better works.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314354.htmlTechArticleSummary of the application of PHP output control function in Simplified and Traditional Chinese conversion: This article provides a brief introduction to the output control function of PHP It also gives specific ideas and examples for its application in Simplified and Traditional Chinese conversion...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn