在网上搜了很多,最后还是在WEBTREES中找到了答案,通过修改,可用作将汉语名字转化成拼音姓名,保存在FONE字段中。

<?php
//将汉字转成拼音

declare(strict_types=1);

class SlugFactory
{
/** @var Transliterator|null $transliterator */
private $transliterator;

public function __construct()
{
if (extension_loaded(‘intl’)) {
$ids = Transliterator::listIDs();

if (in_array(‘Any-Latin’, $ids, true) && in_array(‘Latin-ASCII’, $ids, true)) {
$this->transliterator = Transliterator::create(‘Any-Latin;Latin-ASCII’);
}
}
}

/**
* @param GedcomRecord $record
*
* @return string|null
*/
public function make($record): ?string
{
$slug = strip_tags($record);

if ($this->transliterator instanceof Transliterator) {
$slug = $this->transliterator->transliterate($slug);

if ($slug === false) {
return null;
}
}

$slug = preg_replace(‘/[^A-Za-z0-9]+/’, ‘ ‘, $slug);
$slug = ucwords($slug);
$slug = trim($slug, ‘-‘);

if ($slug !== ”) {
return $slug;
}

return null;
}
}
$py = new SlugFactory();
echo $py->make(“宗谱网”);
echo “<br>”;
echo $py->make(“玉龙陈氏家族”);

?>

2022年5月15日,补充另外一个用JS实现在输入汉语姓名,实时转换成拼音的方法:

<!DOCTYPE html>
<html  xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″/>
<title>汉字姓名转拼音</title>
<script src=”https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@latest/dist/pinyin-pro.js”></script>
</head>
<body>

<script>

var { pinyin } = pinyinPro;

function fullName(){
var firstName = document.getElementById(“firstName”).value;
var lastName = document.getElementById(“lastName”).value;

var pinyinOfFirstName = pinyin(firstName, {
mode: ‘surname’,
toneType: ‘none’,
}).replace(/\s/g,””);

var pinyinOfLastName = pinyin(lastName, {
toneType: ‘none’,
type: ‘array’,
}).join(”);

var result = `${
(pinyinOfFirstName[0] || ”).toUpperCase() + pinyinOfFirstName.substr(1)} ${(pinyinOfLastName[0] || ”).toUpperCase() + pinyinOfLastName.substr(1)}`;

document.getElementById(“pinyinName”).value = result;
}

</script>

<div id=”text”>
<label>姓 氏:</label><input type=”text” onchange=”fullName()” id=”firstName”><br>
<label>名 字:</label><input type=”text” onchange=”fullName()” id=”lastName”><br>
<label>拼音名:</label><input type=”text” id=”pinyinName”><br>
</div>

</body>
</html>