大网数据广告活动
PHP 语言与运行手册

Calculate the metaphone key of a string

· 52CE 我爱测

metaphone — Calculate the metaphone key of a string

metaphone

(PHP 4, PHP 5, PHP 7, PHP 8)

metaphone — Calculate the metaphone key of a string

说明

function metaphone(string $string, int $max_phonemes = 0): string

Calculates the metaphone key of string.

Similar to soundex() metaphone creates the same key for similar sounding words. It's more accurate than soundex() as it knows the basic rules of English pronunciation. The metaphone generated keys are of variable length.

Metaphone was developed by Lawrence Philips <lphilips at verity dot com>. It is described in ["Practical Algorithms for Programmers", Binstock & Rex, Addison Wesley, 1995].

参数

string

The input string.

max_phonemes

This parameter restricts the returned metaphone key to max_phonemes characters in length. However, the resulting phonemes are always transcribed completely, so the resulting string length may be slightly longer than max_phonemes. The default value of 0 means no restriction.

返回值

Returns the metaphone key as a string.

更新日志

版本 说明
8.0.0 The function returned false on failure.

示例

示例 #1 metaphone() basic example

<?php
var_dump(metaphone('programming'));
var_dump(metaphone('programmer'));
?>

以上示例会输出:

string(7) "PRKRMNK"
string(6) "PRKRMR"

示例 #2 Using the max_phonemes parameter

<?php
var_dump(metaphone('programming', 5));
var_dump(metaphone('programmer', 5));
?>

以上示例会输出:

string(5) "PRKRM"
string(5) "PRKRM"

示例 #3 Using the max_phonemes parameter

In this example, metaphone() is advised to produce a string of five characters, but that would require to split the final phoneme ('x' is supposed to be transcribed to 'KS'), so the function returns a string with six characters.

<?php
var_dump(metaphone('Asterix', 5));
?>

以上示例会输出:

string(6) "ASTRKS"

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。