pinyin-util

convert chinese to pinyin

Usage no npm install needed!

<script type="module">
  import pinyinUtil from 'https://cdn.skypack.dev/pinyin-util';
</script>

README

pinyin-util

Introduction

汉字拼音转换工具

字符范围:Unicode字符中4E00(19968)-9FA5(40869)共计20902

Installation

Install with npm:

npm install --save-dev pinyin-util

Install with yarn:

yarn add pinyin-util --dev

Usage

ES6 module:

import pinyinUtil from 'pinyin-util';

pinyinUtil.getPinyin('小明'); // xiaoming

Script:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>pinyin-util</title>
  <script src="pinyin-util.min.js"></script>
</head>
<body>
  <script type="text/javascript">
    pinyinUtil.getPinyin('小明'); // xiaoming
  </script>
</body>
</html>

API

getPinyin(chinese, [splitter=''], [withtone=false])

根据汉字获取拼音

getPinyin('小明', ' '); // xiao ming

getPinyin('小明', ' ', true); // xiǎo míng

getPinyin('小明plus', ' '); // xiao ming plus

getCityPinyin(city, [splitter=''], [withtone=false])

获取城市拼音【城市多音字已处理】

getCityPinyin('重庆市'); // chongqingshi

getCityPinyin('西藏', ' ', true); // xī zhàng

getHanzi(pinyin)

单个拼音转汉字

getHanzi('diu'); // 丟丢銩铥