【发布时间】:2020-09-13 15:56:54
【问题描述】:
对于带有重音和“ñ”的西班牙语名称,我需要获得相同的 ucwords 函数结果,例如“Ángela María Ñusté Fernández”或“José Édgar Ramí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;
}
我想知道是否有更好的方法来解决这个要求。
【问题讨论】:
-
原生的多字节安全函数:stackoverflow.com/a/68768405/2943403
标签: php regex preg-replace special-characters capitalize