Summary: This paper briefly introduces the output control function of PHP and gives some concrete ideas and examples for its application in simplified traditional transformation.
Introduction of a PHP output control function
PHP, as one of the most popular scripting languages, has the advantages of simple writing, fast execution and good extensibility. The output information control function of PHP allows you to control the content of your script output and can be used in a number of different situations, especially where your script has to send a header after it has been exported, and where the output information needs to be edited. The output control function does not affect file header information sent using header () or Setcookie (), only for blocks of data that resemble the 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 if no output information is controlled.
Hello php2000
But by specifying the output control function, the output becomes
Hello y10k
In the above example, the output with Echo () will be saved in the output buffer until the Ob_end_flush () is invoked or the script run terminates, and the output information is processed by the custom handler (replacing the inside string) and returns the result.
Correlation function Description
Ob_start ([string output_callback])-Open output buffer
All output information is not sent directly to the browser, but is stored in the output buffer, which is used to process output information.
Ob_end_flush-End (send) the contents of the output buffer, turn off the output buffer
The realization of two simple traditional conversion
Generally through the form of the table to achieve, the relevant articles are very many, here is not much to say, only 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;
}
?>
which
$GB _big5_ Keep the big5 of the font
$str for the string to be converted
Application of three-output control function in simple traditional transformation
At present, most of the simple traditional page conversion of the site is achieved through their own separate pages, which results in the modification of the simplified page will need to modify the traditional page again, can not be automatic synchronization. And we provide this method, can achieve the same page automatic transformation of simplified traditional display. The implementation method is:
1 to establish a simple traditional logo, to indicate the current display of the simple traditional state, while the simple traditional state of the switch
php2000_gb_big5.php
?
Session_Start (); Turn on the session function to automatically pass a flag between pages
if (!session_is_registered ("Php2000_big5")) {//Check the registration status of the simple traditional logo
Session_register ("Php2000_big5"); Registration simple traditional logo, simplified = 0; traditional =1
$php 2000_big5=0; Default to Simplified
}
$php 2000_big5 = ($php 2000_big5+1)%2; Toggle Simple Traditional State
Header ("Location:". getenv ("Http_referer")); Returns its call page
?>
2 the page output information to control, each page calls this program, for simple traditional conversion
Require.php (should include the previous second part of the conversion code, here slightly)
?
Session_Start ();
function Translate_gb2big5 ($STR) {
$str = Gb2big5 ($STR); Converted to Big5
$str = Str_replace (' charset=gb2312 ', ' Charset=big5 ', $str); Replace character type
Header (' content-type:text/html; Charset=big5 '); Traditional file headers
return $str;
}
if (session_is_registered ("Php2000_big5") && ($php 2000_big5==1)) {//Judgment flag
$fp = fopen (' big5.table ', ' R '); Big5 's Font list
$_gb_big5_ = Fread ($fp, FileSize (' big5.table ')); Read the data
Fclose ($FP);
Ob_start (' Translate_gb2big5 '); Start Output Information Control
}
?>
3 using the method, here is a simple example, placed in the same directory as require.php
test.php
?
Require ("require.php");
echo "Everybody good, this is the PHP century net";
?>
<a href=php2000_gb_big5.php>
?
if ($php 2000_big5==1) echo "GB";
else echo "Big5";
?>
</a>
The first run results are the default simplified
Hello everyone, this is the PHP century network Big5
Click Big5 Connection to display the following traditional
Hello everyone, this is the PHP century network GB
Click GB to return to the simplified page
Because of the use of the session to save the simple traditional logo, so that any other use of require.php pages will automatically follow the current logo display the appropriate page. For more examples please see my website http://www.php2000.com.
An improved method of saving 4 Big5 fonts
Once considered using the session to save the Big5 font, but after the use of the discovery speed significantly slowed down, mainly because the session is also through the form of a file, so will not improve performance, and because the session will not be based on simple traditional signs automatically determine whether the load or not, So the result is also loaded in the simplified big5 of the font, so the speed slowed down.
Because I use a server that is Linux, consider using shared memory (Windows does not support shared memory) to save Big5 font information. The code for the change is the require.php part of the decision:
?
if (session_is_registered ("Php2000_big5") && ($php 2000_big5==1))
{
Modified to use shared memory
Determine if you have created, open 50000-byte shared memory for the 0XFF3 segment
$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 a system identified as 0XFF3
$shm _id = @shmop_open (0XFF3, "C", 0644, 50000);
Read the 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 information on how shared memory is used, refer to the data.
Four conclusions
PHP, as a scripting language that exposes the source code, is a very good extensibility. This article is just a function of one of the application of a way to explore, and achieve a more perfect the same page automatic simplified traditional conversion function. Hope that the broad masses of PHP friends can be inspired to make better works.
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.