I need to get the same ucwords function result for names in spanish having accents and 'ñ', like "Ángela María Ñusté Fernández" or "José Édgar Ramírez Álvarez"
After dealing a lot I get the following algorithm:
/*------------------------------------------------------------------*/
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;
}
I'm wondering if there is a better way to solve this requirement.