Application of P-output control function in simple traditional conversion

Source: Internet
Author: User
Tags ord php code

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, not usually in the case where your script has already exported information 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)
?

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.