心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

汉字转拼音用法.txt 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 引入自定义类 不成功
  2. ======================
  3. 测试引入百度sdk funciton引用报错
  4. ======================
  5. composer类库—汉字转拼音
  6. composer require "overtrue/pinyin:~3.0"
  7. use Overtrue\Pinyin\Pinyin;
  8. // 小内存型,默认,将字典分片载入内存,适用于内存比较紧张的环境,优点:占用内存小,转换不如内存型快
  9. $pinyin = new Pinyin();
  10. // 内存型,将所有字典预先载入内存,适用于服务器内存空间较富余,优点:转换快
  11. // $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  12. // I/O型,不载入内存,将字典使用文件流打开逐行遍历并运用php5.5生成器(yield)特性分配单行内存,适用于虚拟机,内存限制比较严格环境。
  13. // 优点:非常微小内存消耗。缺点:转换慢,不如内存型转换快,php >= 5.5
  14. // $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');
  15. $pinyin->convert('带着希望去旅行,比到达终点更美好');
  16. // ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
  17. $pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_UNICODE);
  18. // ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]
  19. $pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII);
  20. //["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
  21. 第二个参数为可选项,如下:
  22. PINYIN_NONE 不带音调输出: mei hao
  23. PINYIN_ASCII 带数字式音调:mei3 hao3
  24. PINYIN_UNICODE UNICODE 式音调:měi hǎo
  25. 生成用于链接的拼音字符串:
  26. $pinyin->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lv-xing
  27. $pinyin->permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lv.xing
  28. 获取首字符字符串:
  29. $pinyin->abbr('带着希望去旅行'); // dzxwqlx
  30. $pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x
  31. 翻译整段文字为拼音:
  32. 将会保留中文字符:,。 ! ? : “ ” ‘ ’ 并替换为对应的英文符号。
  33. $pinyin->sentence('带着希望去旅行,比到达终点更美好!');
  34. // dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao!
  35. $pinyin->sentence('带着希望去旅行,比到达终点更美好!', true);
  36. // dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
  37. 翻译姓名:
  38. 姓名的姓的读音有些与普通字不一样,比如 ‘单’ 常见的音为 dan,而作为姓的时候读 shan。
  39. $pinyin->name('单某某'); // ['shan', 'mou', 'mou']
  40. $pinyin->name('单某某', PINYIN_UNICODE); // ["shàn","mǒu","mǒu"]