5

是否有可能

A. 找出一个字符是否是中文(简体),在这种情况下
B. 得到拼音?例如: 你好 => nǐhǎo 使用 java 还是 php?

干杯

4

3 回答 3

6

一)
是的。以 unicode 表示的所有字符都有一个唯一的数字索引,称为codepoint

如果您知道简体中文的码点范围,并且知道如何获取给定字符的 unicode 码点,那么简单的比较就会告诉您给定字符是否在简体中文范围内。

一个现有的问题有一个在 PHP 中获取字符的 unicode 代码点的解决方案:
How to get code point number for a given character in a utf-8 string?

在 Java 中,静态 java.lang。Character::codePointAt()方法会给你你所需要的。

B)
将简体中文字符或字符串转换为拼音很可能需要某种形式的映射,其中 unicode 代码点作为键,对应的拼音作为值。

PHP 中的一个示例显示在http://kingphp.com/108.html

对 [java pinyin] 进行简单的 Google 搜索会显示一系列选项,其中两个是http://kiang.org/jordan/software/pinyinime/http://pinyin4j.sourceforge.net/上的中文到拼音库。

于 2010-06-29T18:46:48.273 回答
3

有点晚了,但解决了!

<?php

function curl($url,$params = array(),$is_coockie_set = false)
{

if(!$is_coockie_set){
/* STEP 1. let¡¯s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}

$str = ''; $str_arr= array();
foreach($params as $key => $value)
{
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);

/* STEP 3. visit cookiepage.php */

$Url = $url.$str;

$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec ($ch);
return $output;
}

function Translate($word,$from,$to)
{
$word = urlencode($word);
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=' . $from . '&sl=' . $from . '&tl=' . $to . '&ie=UTF-8&oe=UTF-8&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';

$name_en = curl($url);
$name_en = explode('"',$name_en);
return $name_en[1];
}
function pinyin($word)
{
$word = urlencode($word);
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=zh&sl=zh&tl=zh&ie=UTF-8&oe=UTF-8&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';

$name_en = curl($url);
$name_en = explode('"',$name_en);
return str_replace(" ", "", strtolower($name_en[5]));
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo pinyin(urldecode($_GET['phrase']));
?>
</body>
</html>

如果你把它放在http://www.example.com/foo.php,输入http://www.example.com/foo.php?phrase=你好,它会给你拼音。

经测试,有效。

于 2012-11-19T08:40:13.777 回答
-1

如果您使用 utf-8 来解释您的文件并调用数据库,我想一个简单的

$new_text = preg_replace(array('/你好/',...), array('nǐhǎo',...), $old_text);

应该做的伎俩。

你从哪里得到你的字符串?

于 2010-06-29T18:47:06.593 回答