5

Is it possible to

A. find out if a character is Chinese (simplified) and in that case
B. get the pinyin? example: 你好 => nǐhǎo using java or php?

Cheers

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Moak
  • 12,596
  • 27
  • 111
  • 166

3 Answers3

6

A)
Yes. All characters represented in unicode have a unique numeric index called a codepoint.

If you know the range of codepoints for simplified Chinese and you know how to get the unicode codepoint of a given character, a simple comparison will tell you if the given character is within the simplified Chinese range.

An existing question has a solution for getting the unicode codepoint for a character in PHP:
How to get code point number for a given character in a utf-8 string?

In Java, the static java.lang.Character::codePointAt() method will give you what you need.

B)
Converting a simplified Chinese character, or string, to Pinyin would most likely require some form of map with the unicode code point as the key and the corresponding pinyin as the value.

An example of this in PHP is shown at http://kingphp.com/108.html.

A simple Google search for [java pinyin] reveals a range of options, two of which being Chinese to pinyin libraries at http://kiang.org/jordan/software/pinyinime/ and http://pinyin4j.sourceforge.net/.

Community
  • 1
  • 1
Jon Cram
  • 16,609
  • 24
  • 76
  • 107
  • Thanls for all that, I'll take it from here ;) was googling for pinyin php and the results weren't that great, however just added the java tag because I just started learning so I didn't think to google it. – Moak Jun 30 '10 at 05:00
  • the kingphp.com code is a little bit stuffed-up, so could you consider re-posting a link? – Lucas Nov 19 '12 at 07:50
  • @think123: I'm not sure what you mean by that. I posted a link to an example to demonstrate that what needs to be achieved can be achieved. The link still shows this. – Jon Cram Nov 27 '12 at 15:38
  • @think123 That's fine, it's an example. It's not a robust solution, it simply demonstrates that what needs to be achieved can be achieved. – Jon Cram Nov 28 '12 at 11:00
3

Bit late, but solved!

<?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>

If you put this at http://www.example.com/foo.php, type in http://www.example.com/foo.php?phrase=你好, and it will give you the pinyin.

Tested, and works.

Lucas
  • 16,930
  • 31
  • 110
  • 182
-1

If you are using utf-8 to interpret your files and calls to the DB, i guess a simple

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

should do the trick.

Where are you getting your string from?

misterte
  • 977
  • 1
  • 11
  • 21
  • sorry if it was unclear, I need the pinyin from any Chinese characters. In this case to translate names. – Moak Jun 30 '10 at 04:35