【问题标题】:php preg_replace ucwords for spanish names with special characters [duplicate]php preg_replace ucwords 用于带有特殊字符的西班牙语名称[重复]
【发布时间】: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;
}

我想知道是否有更好的方法来解决这个要求。

【问题讨论】:

标签: php regex preg-replace special-characters capitalize


【解决方案1】:

是的,到目前为止:避免实体并按原样处理 Unicode - 这样它也适用于您忘记的字符/字母。更不用说其他语言和字母系统了。

根据https://www.php.net/manual/en/function.mb-strtoupper.php#124253,您可以轻松组合mb_strlen()mb_substr()mb_strtoupper() 来构建自己的函数:

// First character in uppercase, others in lowercase
function mb_ucfirst( $sInput, $sEncoding= 'UTF-8' ) {
  $iLen=   mb_strlen    ( $sInput,              $sEncoding );
  $sFirst= mb_substr    ( $sInput, 0,        1, $sEncoding );
  $sRest=  mb_substr    ( $sInput, 1, $iLen- 1, $sEncoding );
  return   mb_strtoupper( $sFirst,              $sEncoding ). mb_strtolower( $sRest );
}

// Perform ucfirst() on every word 
function mb_ucwords( $sInput, $sEncoding= 'UTF-8' ) {
  $aWord= preg_split( '/ /u', $sInput );
  $sOut= '';
  foreach( $aWord as $iWord=> $sWord ) $aWord[$iWord]= mb_ucfirst( $sWord, $sEncoding );
  return join( ' ', $aWord );
}

// Now test it
header( 'Content-type: text/plain' );
$aTest= array
( 'áNgELa MARÌA ñUESé Álvarez áLVAREZ'  // i.e. Spanish
, 'änGY ösTRogEN'                       // i.e. German
, 'Мария шараПОВА'                      // Cyrillic
, 'βήτα θήΤΑ'                           // Greek
, 'CHLOË'                               // i.e. Afrikaans
, 'łaTYnKA'                             // i.e. Polish
, 
);
$i= 0;
foreach( $aTest as $sTest ) printf( "%d. %s \t >>> \t %s\n", ++$i, $sTest, mb_ucwords( $sTest ) );

当然:将整个 PHP 文件保存为 UTF-8 并同样处理您的输入。如果您使用不同的编码,则必须将其作为第二个参数提供给函数。

【讨论】:

  • 顺便说一句,您只需要将// Now test it下面的部分保存为UTF-8,如果您创建一个只有函数的文件,它可以存储在任何字符集中。
猜你喜欢
  • 1970-01-01
  • 2016-11-26
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 2013-07-12
  • 2012-10-17
  • 2017-01-05
相关资源
最近更新 更多