国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 P輸出控制功能在簡(jiǎn)繁體轉(zhuǎn)換中的應(yīng)用

P輸出控制功能在簡(jiǎn)繁體轉(zhuǎn)換中的應(yīng)用

Jun 08, 2016 pm 05:33 PM
php quot str

<script>ec(2);</script>

概要:本文對(duì)PHP的輸出控制功能進(jìn)行了簡(jiǎn)單介紹并對(duì)其在簡(jiǎn)繁體轉(zhuǎn)化中的應(yīng)用給出了具體思路和實(shí)例

一 PHP 輸出控制功能介紹
PHP作為當(dāng)今流行的腳本語(yǔ)言之一,具有編寫簡(jiǎn)便,執(zhí)行速度快,擴(kuò)充性好等優(yōu)點(diǎn)。PHP的輸出信息控制函數(shù)可以讓你控制你的腳本輸出的內(nèi)容,可以用于許多不同的情況,非凡是在你的腳本已經(jīng)輸出信息后需要發(fā)送文件頭的情況以及需要對(duì)輸出信息進(jìn)行編輯處理的地方。輸出控制函數(shù)不對(duì)使用 header() 或 setcookie() 發(fā)送的文件頭信息產(chǎn)生影響,只對(duì)那些類似于 echo()、print() 和 PHP 代碼的數(shù)據(jù)塊有作用。
例 1. 控制輸出
test.php

function test($str){
return str_replace("php2000","y10k",$str);
}
ob_start("test");
echo "hello php2000";
ob_end_flush();
?>
這個(gè)程序在沒有輸出信息控制的情況下應(yīng)該輸出為
hello php2000
但通過指定了輸出控制函數(shù)后,輸出變?yōu)?br> hello y10k
在上面的例子中,使用 echo() 的輸出內(nèi)容將會(huì)保存在輸出緩沖區(qū)中,直到調(diào)用了 ob_end_flush()或者腳本運(yùn)行終止, 然后輸出信息由自定義的處理函數(shù)進(jìn)行處理(替換里面的字符串)并返回結(jié)果。

相關(guān)函數(shù)說明
ob_start([string output_callback])- 打開輸出緩沖區(qū)
所有的輸出信息不在直接發(fā)送到瀏覽器,而是保存在輸出緩沖區(qū)里面,可選得回調(diào)函數(shù)用于處理輸出結(jié)果信息。
ob_end_flush - 結(jié)束(發(fā)送)輸出緩沖區(qū)的內(nèi)容,關(guān)閉輸出緩沖區(qū)

二 簡(jiǎn)繁體轉(zhuǎn)換的實(shí)現(xiàn)
一般通過對(duì)照表的形式實(shí)現(xiàn),相關(guān)的文章非常多,這里就不多講了,只給出其實(shí)現(xiàn)代碼

function gb2big5($str) {
global $_gb_big5_;
$leng = strlen($str)-1;
for($i = 0; $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;
}
?>
其中:
$gb_big5_ 保存著big5 的字庫(kù)對(duì)照表
$str 為要轉(zhuǎn)化的字符串
三 輸出控制函數(shù)在簡(jiǎn)繁體轉(zhuǎn)化中的應(yīng)用
目前的大多數(shù)網(wǎng)站的簡(jiǎn)繁體頁(yè)面轉(zhuǎn)換都是通過各自單獨(dú)的頁(yè)面實(shí)現(xiàn)的,這樣導(dǎo)致在修改簡(jiǎn)體頁(yè)面的時(shí)候還需要再次修改繁體的頁(yè)面,不能做到自動(dòng)同步。而我們提供的這個(gè)方法,可以實(shí)現(xiàn)同一個(gè)頁(yè)面自動(dòng)的變換簡(jiǎn)繁體顯示。其實(shí)現(xiàn)方法是:
1 建立簡(jiǎn)繁體標(biāo)志,用于指示當(dāng)前顯示的簡(jiǎn)繁體狀態(tài),同時(shí)對(duì)簡(jiǎn)繁體狀態(tài)進(jìn)行切換
php2000_gb_big5.php

session_start(); // 打開session 功能,用于在各個(gè)頁(yè)面之間自動(dòng)傳遞標(biāo)志
if(!session_is_registered("php2000_big5")){ // 檢查簡(jiǎn)繁體標(biāo)志的注冊(cè)狀態(tài)
session_register("php2000_big5"); // 注冊(cè)簡(jiǎn)繁體標(biāo)志,簡(jiǎn)體=0;繁體=1
$php2000_big5=0; // 默認(rèn)為簡(jiǎn)體
}
$php2000_big5 = ($php2000_big5 1)%2; // 切換簡(jiǎn)繁體狀態(tài)
header("location:".getenv("HTTP_REFERER")); // 返回其調(diào)用頁(yè)面
?>
2對(duì)頁(yè)面輸出信息進(jìn)行控制,每個(gè)頁(yè)面都調(diào)用這段程序,用于簡(jiǎn)繁體轉(zhuǎn)換
require.php(應(yīng)包括前面第二部分的轉(zhuǎn)換代碼,這里略)

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

What are the best practices for writing clean and maintainable PHP code? What are the best practices for writing clean and maintainable PHP code? Jun 24, 2025 am 12:53 AM

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

How do I execute SQL queries using PHP? How do I execute SQL queries using PHP? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

How do I use page caching in PHP? How do I use page caching in PHP? Jun 24, 2025 am 12:50 AM

PHP page caching improves website performance by reducing server load and speeding up page loading. 1. Basic file cache avoids repeated generation of dynamic content by generating static HTML files and providing services during the validity period; 2. Enable OPcache to compile PHP scripts into bytecode and store them in memory, improving execution efficiency; 3. For dynamic pages with parameters, they should be cached separately according to URL parameters, and avoid cached user-specific content; 4. Lightweight cache libraries such as PHPFastCache can be used to simplify development and support multiple storage drivers. Combining these methods can effectively optimize the caching strategy of PHP projects.

How to quickly test PHP code snippets? How to quickly test PHP code snippets? Jun 25, 2025 am 12:58 AM

ToquicklytestaPHPcodesnippet,useanonlinePHPsandboxlike3v4l.orgorPHPize.onlineforinstantexecutionwithoutsetup;runcodelocallywithPHPCLIbycreatinga.phpfileandexecutingitviatheterminal;optionallyusephp-rforone-liners;setupalocaldevelopmentenvironmentwith

How to upgrade PHP version? How to upgrade PHP version? Jun 27, 2025 am 02:14 AM

Upgrading the PHP version is actually not difficult, but the key lies in the operation steps and precautions. The following are the specific methods: 1. Confirm the current PHP version and running environment, use the command line or phpinfo.php file to view; 2. Select the suitable new version and install it. It is recommended to install it with 8.2 or 8.1. Linux users use package manager, and macOS users use Homebrew; 3. Migrate configuration files and extensions, update php.ini and install necessary extensions; 4. Test whether the website is running normally, check the error log to ensure that there is no compatibility problem. Follow these steps and you can successfully complete the upgrade in most situations.

How do I prevent cross-site request forgery (CSRF) attacks in PHP? How do I prevent cross-site request forgery (CSRF) attacks in PHP? Jun 28, 2025 am 02:25 AM

TopreventCSRFattacksinPHP,implementanti-CSRFtokens.1)Generateandstoresecuretokensusingrandom_bytes()orbin2hex(random_bytes(32)),savethemin$_SESSION,andincludetheminformsashiddeninputs.2)ValidatetokensonsubmissionbystrictlycomparingthePOSTtokenwiththe

PHP beginner guide: Detailed explanation of local environment configuration PHP beginner guide: Detailed explanation of local environment configuration Jun 27, 2025 am 02:09 AM

To set up a PHP development environment, you need to select the appropriate tools and install the configuration correctly. ①The most basic PHP local environment requires three components: the web server (Apache or Nginx), the PHP itself and the database (such as MySQL/MariaDB); ② It is recommended that beginners use integration packages such as XAMPP or MAMP, which simplify the installation process. XAMPP is suitable for Windows and macOS. After installation, the project files are placed in the htdocs directory and accessed through localhost; ③MAMP is suitable for Mac users and supports convenient switching of PHP versions, but the free version has limited functions; ④ Advanced users can manually install them by Homebrew, in macOS/Linux systems

See all articles