对于带有重音和'ñ'的西班牙语名称,我需要获得相同的ucwords函数结果,例如“ ÁngelaMaríaÑustéFernández”或“ JoséÉdgarRamírezÁlvarez”]
经过大量处理后,我得到以下算法:
/*------------------------------------------------------------------*/
function html2ucwords($texto)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DESCRIPCION:
Pone la primera letra de cada palabra en mayúscula teniendo en cuenta
las tildes.
--------------------------------------------------------------------*/
{
$retorno = htmlentities($texto);
$retorno = strtolower($retorno);
$retorno = ucwords($retorno);
$retorno = preg_split("# #", $retorno);
$tilinc = array( '#^á#'
, '#^é#'
, '#^í#'
, '#^ó#'
, '#^ú#'
, '#^ñ#'
);
$mayusculas = array( 'Á'
, 'É'
, 'Í'
, 'Ó'
, 'Ú'
, 'Ñ'
);
foreach ($retorno as $llave => $valor)
$retorno[$llave] = preg_replace($tilinc, $mayusculas, $valor);
$retorno = join(" ", $retorno);
$retorno = html_entity_decode($retorno);
return $retorno;
}
我想知道是否有更好的方法来解决此要求。
是的,到目前为止:避免使用实体,并按原样对待Unicode-这样,它也适用于您忘记的字符/字母。更不用说其他语言和字母系统。
根据https://www.php.net/manual/en/function.mb-strtoupper.php#124253,您可以轻松组合mb_strlen()
,mb_substr()
和mb_strtoupper()
以建立自己的功能: