格式化人名-PHP(或任何语言)库?

格式化人名-PHP(或任何语言)库?,php,formatting,string-formatting,Php,Formatting,String Formatting,有没有办法格式化人名?例如,“joHn dOE”应该是“joHn dOE”。或者“安格斯·麦基弗”应该是“安格斯·麦基弗”。等等 我知道任何解决方案都不可能是完整的(名字有太多的规则),但总比没有好。有什么建议吗?如评论中所述,在PHP中,您可以执行以下操作: $name_formatted = ucfirst(strtolower($name_unformatted)); <?php $name_unformatted = "JOHN DOE"; function

有没有办法格式化人名?例如,“joHn dOE”应该是“joHn dOE”。或者“安格斯·麦基弗”应该是“安格斯·麦基弗”。等等


我知道任何解决方案都不可能是完整的(名字有太多的规则),但总比没有好。有什么建议吗?

如评论中所述,在PHP中,您可以执行以下操作:

$name_formatted = ucfirst(strtolower($name_unformatted));
<?php
$name_unformatted = "JOHN DOE";

function format_name($name_unformatted)
{
   $name_formatted = ucwords(strtolower($name_unformatted));  // this will handle 90% of the names

   // ucwords will work for most strings, but if you wanted to break out each word so you can deal with exceptions, you could do something like this:
   $separator = array(" ","-","+","'");
   foreach($separator as $s)
   {
      if (strpos($name_formatted, $s) !== false)
      {
         $word = explode($s, $name_formatted);
         $tmp_ary = array_map("ucfirst", array_map("strtolower", $word));  // whatever processing you want to do here
         $name_formatted = implode($s, $tmp_ary);
      }
   }

   return $name_formatted;
}

echo format_name($name_unformatted);
?>
这将处理90%的案件。然后,我将把它放到一个函数中,并添加规则来处理MacGuyver,O'Reilly类型的异常

更新: 如前所述,ucfirst只处理字符串中的第一个单词。您可以使用正则表达式将每个单词的所有首字母大写,或者执行如下功能:

$name_formatted = ucfirst(strtolower($name_unformatted));
<?php
$name_unformatted = "JOHN DOE";

function format_name($name_unformatted)
{
   $name_formatted = ucwords(strtolower($name_unformatted));  // this will handle 90% of the names

   // ucwords will work for most strings, but if you wanted to break out each word so you can deal with exceptions, you could do something like this:
   $separator = array(" ","-","+","'");
   foreach($separator as $s)
   {
      if (strpos($name_formatted, $s) !== false)
      {
         $word = explode($s, $name_formatted);
         $tmp_ary = array_map("ucfirst", array_map("strtolower", $word));  // whatever processing you want to do here
         $name_formatted = implode($s, $tmp_ary);
      }
   }

   return $name_formatted;
}

echo format_name($name_unformatted);
?>

如评论中所述,在PHP中,您可以执行以下操作:

$name_formatted = ucfirst(strtolower($name_unformatted));
<?php
$name_unformatted = "JOHN DOE";

function format_name($name_unformatted)
{
   $name_formatted = ucwords(strtolower($name_unformatted));  // this will handle 90% of the names

   // ucwords will work for most strings, but if you wanted to break out each word so you can deal with exceptions, you could do something like this:
   $separator = array(" ","-","+","'");
   foreach($separator as $s)
   {
      if (strpos($name_formatted, $s) !== false)
      {
         $word = explode($s, $name_formatted);
         $tmp_ary = array_map("ucfirst", array_map("strtolower", $word));  // whatever processing you want to do here
         $name_formatted = implode($s, $tmp_ary);
      }
   }

   return $name_formatted;
}

echo format_name($name_unformatted);
?>
这将处理90%的案件。然后,我将把它放到一个函数中,并添加规则来处理MacGuyver,O'Reilly类型的异常

更新: 如前所述,ucfirst只处理字符串中的第一个单词。您可以使用正则表达式将每个单词的所有首字母大写,或者执行如下功能:

$name_formatted = ucfirst(strtolower($name_unformatted));
<?php
$name_unformatted = "JOHN DOE";

function format_name($name_unformatted)
{
   $name_formatted = ucwords(strtolower($name_unformatted));  // this will handle 90% of the names

   // ucwords will work for most strings, but if you wanted to break out each word so you can deal with exceptions, you could do something like this:
   $separator = array(" ","-","+","'");
   foreach($separator as $s)
   {
      if (strpos($name_formatted, $s) !== false)
      {
         $word = explode($s, $name_formatted);
         $tmp_ary = array_map("ucfirst", array_map("strtolower", $word));  // whatever processing you want to do here
         $name_formatted = implode($s, $tmp_ary);
      }
   }

   return $name_formatted;
}

echo format_name($name_unformatted);
?>

我正在寻找一个能够处理名称大小写的php脚本。虽然我意识到100%的案件很难处理

我认为这个脚本可以很好地处理95%的用例,至少对我们来说是这样。这当然是一个很好的起点


我正在寻找一个php脚本,它可以处理名称的正确大写。虽然我意识到100%的案件很难处理

我认为这个脚本可以很好地处理95%的用例,至少对我们来说是这样。这当然是一个很好的起点


关于意大利名字,我找到了一个简单的解决办法

函数formatName(字符串$firstName,字符串$lastName):数组
{
$delimiters=“-”\t\r\n\f\v”;
返回\数组\映射(
fn($string)=>\ucwords(\mb\u strtolower($string),$delimiters),
[$firstName,$lastName]
);
}
在我的情况下,我不得不处理诸如
D'Amico
De Angelis
Di Cataldo
Rossi Bianchi
这样的姓氏,而这个快速解决方案效果很好

主要参与者是

\ucwords(\mb_strtolower($string), $delimiters)

剩下的只是我如何通过将名字和姓氏分开来处理名字的一部分。

关于意大利名字的问题,我找到了一个简单的解决方法

函数formatName(字符串$firstName,字符串$lastName):数组
{
$delimiters=“-”\t\r\n\f\v”;
返回\数组\映射(
fn($string)=>\ucwords(\mb\u strtolower($string),$delimiters),
[$firstName,$lastName]
);
}
在我的情况下,我不得不处理诸如
D'Amico
De Angelis
Di Cataldo
Rossi Bianchi
这样的姓氏,而这个快速解决方案效果很好

主要参与者是

\ucwords(\mb_strtolower($string), $delimiters)


剩下的只是我用名字和姓氏分开来处理名字的一部分。

你想加一个大写字母,剩下的小写字母吗?
ucfirst
和一个词缀列表会很有用。这是众所周知的20%的努力,不用麻烦了。要么让它完全按照用户输入的方式运行,要么通过一个简单的
ucfirst()
。在Python中:
x='johndoe';x、 title()。这是众所周知的20%的努力,不用麻烦了。要么让它完全按照用户输入的方式运行,要么通过一个简单的
ucfirst()
。在Python中:
x='johndoe';x、 title()。我将用一个函数更新我的答案,该函数将执行所有单词。DOH!忘记了PHP中的ucwords函数。我看到了你函数的优点。虽然我意识到它不会处理所有可能的名称情况(因为规则只是指数级的),但我认为这已经是一个很好的开始。我对您用作单词分隔符的
+
符号很好奇。你有一个使用
+
的名字的例子吗?我从php网站上抓取了这段代码,我想-你可以删除+,因为它可能不在你的场景中使用。如果你觉得我的回答有用,请接受。谢谢
ucfirst
不会为JOHN DOE做任何事情。是和否@snoopy76-我的代码用小写字母表示整个字符串,然后用大写字母表示第一个字母。我将用一个函数更新我的答案,该函数将执行所有单词。DOH!忘记了PHP中的ucwords函数。我看到了你函数的优点。虽然我意识到它不会处理所有可能的名称情况(因为规则只是指数级的),但我认为这已经是一个很好的开始。我对您用作单词分隔符的
+
符号很好奇。你有一个使用
+
的名字的例子吗?我从php网站上抓取了这段代码,我想-你可以删除+,因为它可能不在你的场景中使用。如果你觉得我的回答有用,请接受。谢谢