PHP to convert Simplified Chinese characters to traditional methods

Source: Internet
Author: User
Tags array contains count mysql php file access database access linux

Recently, a project involving the conversion of Simplified Chinese characters to Traditional Chinese characters, online search, search for a long time, found that no one can use, but ultimately it is a process of understanding. The idea of most articles on the internet is to find the corresponding Chinese characters in a simplified, traditional corresponding table, and then to replace them, the principle is so simple.

No way, can not find ready-made, you have to stand on the shoulders of predecessors to create their own. What makes me feel comfortable is to find a Chinese simplified, traditional corresponding data table, according to their own ideas to organize it into a key in simplified characters, traditional characters for the value of a one-dimensional array, similar to the following array structure:

$data =array (' side ' => ' flank ', ' factory ' => ' plant ');

Well, according to this structure, the corresponding replacement method is as follows:

function j2f ($str) {
	if (trim ($str) = = ') {return
		';
	}
	$fstr = ';
    Include ' j2fdata.php '///This data file has the download address
	$count =mb_strlen ($str, ' utf-8 ');
    For ($i =0 $i <= $count; $i + +) {
        $jchar =mb_substr ($str, $i, 1, ' utf-8 ');
		The $fantidata array used here is the $fchar =isset defined in the j2fdata.php file
        ($fantiData [$jchar])? $fantiData [$jchar]: $jchar;
        $fstr. = $fchar;
    }
    return $fstr;
}

The specific usage is as follows:

<?php
$str = ' This site is a PHP Programmer's working life notes, this site contains the PHP Network programming Learning tutorials, database (mainly MySQL and Access database) tutorials, JAVASCRIPT,JQUERY,DIV+CSS, Html,windows systems, Linux systems, and health-care learning notes and the accumulation of life experience! ';
echo j2f ($STR);

The results will be output as follows:

This site is a PHP program member's work life note, this site contains the PHP network learning tutorial, data library (mainly MySQL and Access data library) tutorial, javascript,jquery,div+css,html,windows system, The Linux system as well as the health and nutrition learning notes and life experience of the cumulative!

PHP Simplified => Traditional array data file download



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.