如何获得姓氏首字母

发布于 2024-11-03 20:28:08 字数 324 浏览 2 评论 0原文

可能的重复:
正则表达式 - 返回名字和姓氏

嗨,

我有用户的全名存储在一个字段中。我怎样才能获得完整的名字和姓氏的名字首字母。这就是我现在正在使用的:

 $first = substr($full_n, 0, strpos($full_n,' '));  

Possible Duplicate:
Regex - Return First and Last Name

Hi,

I have the full name of users stored in one field. How can i get the full first name and last name first initial. This is what i am using right now:

 $first = substr($full_n, 0, strpos($full_n,' '));  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绾颜 2024-11-10 20:28:08
list ($first, $last) = explode(' ', $full_n, 2);
echo "$first {$last[0]}.";

与所有其他解决方案一样,也不适用于两个以上的名称。如果你想用它做更多的事情,而不仅仅是显示,你应该单独保存所有(名字,中间名,姓氏,...)。

更新:

受到 Pekka 的启发(问题的评论;))

$names = explode(' ', $full_n);
$first = array_shift($names);
$last = array_pop($names);
echo "$first {$last[0]}.";

这适用于任意(> = 2)的名称计数,但只会显示第一个(完整)和最后一个(缩短)。像“Schmidt-Mueller”这样的东西看起来会很奇怪(“S.”)。

list ($first, $last) = explode(' ', $full_n, 2);
echo "$first {$last[0]}.";

Will not work with more than two names too, like all the other solutions. You should save all (firstname, middle name, surname, ...) separatly, if you want to do more with it, than just displaying.

Update:

Inspired be Pekka (comments of the question ;))

$names = explode(' ', $full_n);
$first = array_shift($names);
$last = array_pop($names);
echo "$first {$last[0]}.";

This works for an arbitrary (>= 2) count of names, but will only display the first (complete) and the last (shortened). Something like "Schmidt-Mueller" will look curious ("S.").

落花随流水 2024-11-10 20:28:08

好吧,您几乎就在那里...

如果保证您的用户将拥有两个名称(只有名字和姓氏,没有空格)任一),那么你可以这样做:

$full_n = 'Demian Brecht';
$first = substr($full_n, 0, strpos($full_n,' ')+2);  
print_r($first);

但是,如果你正在处理 > 2个名字,那么你就得重新思考你的逻辑了。

Well, you're almost there...

If it's guaranteed that your users will have two names (just first and last, with no spaces in either), then you could just do this:

$full_n = 'Demian Brecht';
$first = substr($full_n, 0, strpos($full_n,' ')+2);  
print_r($first);

However, if you're dealing with > 2 names, then you'll have to rethink your logic.

仄言 2024-11-10 20:28:08

(不是答案,但太大而无法评论)

这个问题没有解决方案,你只需要决定以什么方式破坏人们的名字。

答案中已经有几个不错的例子,但它们在佩卡评论中的例子中都失败了。有些名字,例如Hillary Rodham Clinton,通常会连字符为Hillary Rodham-Clinton,这样问题就不那么严重了。

更简单的常见示例,例如 John von Neumann 或 Phillip de la Cruz 仍然会失败。

(not an answer but too big to be a comment)

There is no solution to this problem, you will just have to decide in what way will you mangle peoples names.

There are several decent examples in the answers already, but they will all fail on Pekka's examples in his comment. Some names like Hillary Rodham Clinton are often hyphenated Hillary Rodham-Clinton and will be less of a problem.

Simpler common examples such as John von Neumann or Phillip de la Cruz will still fail.

情绪失控 2024-11-10 20:28:08
$full_n = trim($full_n); // remove extra spaces

$first_name = substr($full_n, 0, strpos($full_n, ' '));
$last_initial = substr($full_n, strrchr($full_n, ' '), 1);

$result = trim($first_name . ' ' . $last_initial);
$full_n = trim($full_n); // remove extra spaces

$first_name = substr($full_n, 0, strpos($full_n, ' '));
$last_initial = substr($full_n, strrchr($full_n, ' '), 1);

$result = trim($first_name . ' ' . $last_initial);
梦在深巷 2024-11-10 20:28:08

我很难理解这个问题。如果您想要完整的名字和姓氏的第一个首字母。

$firstwithlastnameinitial = substr($full_n, 0, strpos($full_n, ' ')) . substr($full_n, strpos($full_n, ' ') + 1, 1);

或者

list ($fname, $lname) = explode(' ', $full_n, 2);
$lastwithfirstinitial = $fname . substr($lname, 0, 1);

如果您想要完整的姓氏和名字的第一个首字母:

$lastwithfirstinitial = substr($full_n, strpos($full_n, ' ') + 1, strlen($full_n)) . substr($full_n, 0, 1);

这应该检索姓氏加上第一个首字母。或者更容易阅读的东西。

list ($fname, $lname) = explode(' ', $full_n, 2);
$lastwithfirstinitial = $lname . substr($fname, 0, 1);

I had a hard time understanding this question. If you want the full first name and the first initial of the last name.

$firstwithlastnameinitial = substr($full_n, 0, strpos($full_n, ' ')) . substr($full_n, strpos($full_n, ' ') + 1, 1);

OR

list ($fname, $lname) = explode(' ', $full_n, 2);
$lastwithfirstinitial = $fname . substr($lname, 0, 1);

If you want the full last name with first initial of the first name:

$lastwithfirstinitial = substr($full_n, strpos($full_n, ' ') + 1, strlen($full_n)) . substr($full_n, 0, 1);

This should retrieve the last name plus the first initial. Or something easier to read.

list ($fname, $lname) = explode(' ', $full_n, 2);
$lastwithfirstinitial = $lname . substr($fname, 0, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文