【问题标题】:Get the first letter of only first and last name in php在php中获取只有名字和姓氏的第一个字母
【发布时间】:2020-08-06 16:56:16
【问题描述】:

我想仅使用用户名字的第一个字母和姓氏的第一个字母来显示用户的首字母缩写。 (测试用户 = TU)

即使用户输入前缀或中间名,我如何才能达到此结果? (Mr. Test Middlename User = TU)。

这是我目前的代码(但会根据用户输入显示超过 2 个字母):

public function initials() {
    $words = explode(" ", $this->name );
    $initials = null;
    foreach ($words as $w) {
        $initials .= $w[0];
    }
    return strtoupper($initials);
}

【问题讨论】:

  • 你怎么知道一个名字是否有前缀?
  • 如果有前缀,我需要添加一些代码来自动删除前缀。也许我可以创建一个包含所有可能前缀的数组,然后从用户名字符串中删除它们(如果它包含任何前缀)?
  • 是的,但是您将不得不做出很多假设,Kiefer William Frederick Dempsey George Rufus SutherlandMrs. Carolina Maria de Jesus 呢?
  • @AbraCadaver 是的,我知道不可能预测每个用户会输入什么作为他们的名字,但我可以接受将“Kiefer William Frederick Dempsey George Rufus Sutherland”转换为“KS”。我也同意将“Bob Sutherland”转换为“BS”。大声笑
  • 超出了您的问题范围,但尝试使用动词作为您的方法名称,例如getIntials ...

标签: php arrays string character


【解决方案1】:

有太多变体,但这应该捕获字符串中的名字和姓氏,这些字符串可能有也可能没有以句点结尾的前缀或后缀:

public function initials() {
    preg_match('/(?:\w+\. )?(\w+).*?(\w+)(?: \w+\.)?$/', $this->name, $result);
    return strtoupper($result[1][0].$result[2][0]);
}

$result[1]$result[2] 是第一个和最后一个捕获组,每个捕获组的 [0] 索引是字符串的第一个字符。

查看Example

这做得很好,但是其中带有空格的名称只会返回第二部分,例如De Jesus 只会返回Jesus。您可以为de, von, van 等姓氏添加已知修饰符,但祝您好运,特别是因为它变得更长van de, van der, van den

要为非英语前缀和后缀扩展它,您可能需要定义它们并将它们去掉,因为有些可能不会以句点结尾。

$delete = ['array', 'of prefixes', 'and suffixes'];
$name = str_replace($delete, '', $this->name);

//or just beginning ^ and end $
$prefix = ['array', 'of prefixes'];
$suffix = ['array', 'of suffixes'];
$name = preg_replace("/^$prefix|$suffix$/", '', $this->name);

【讨论】:

    【解决方案2】:

    您可以使用reset()end() 来实现此目的

    reset() 将数组的内部指针倒回到第一个元素并返回第一个数组元素的值。

    end() 将数组的内部指针前进到最后一个元素,并返回它的值。

    public function initials() {
    
     //The strtoupper() function converts a string to uppercase.
        $name  = strtoupper($this->name); 
        //prefixes that needs to be removed from the name
        $remove = ['.', 'MRS', 'MISS', 'MS', 'MASTER', 'DR', 'MR'];
        $nameWithoutPrefix=str_replace($remove," ",$name);
    
    $words = explode(" ", $nameWithoutPrefix);
    
    //this will give you the first word of the $words array , which is the first name
     $firtsName = reset($words); 
    
    //this will give you the last word of the $words array , which is the last name
     $lastName  = end($words);
    
     echo substr($firtsName,0,1); // this will echo the first letter of your first name
     echo substr($lastName ,0,1); // this will echo the first letter of your last name
    
    }
    

    【讨论】:

    • 据我了解,如果用户输入“Mr. Test Middlename User”,这将返回“MU”,这不是我想要的结果。也许我需要添加一些代码来自动删除前缀?
    • @JakeScervino 那么您的预期结果是什么?
    • “即使用户输入了前缀或中间名,我怎样才能达到这个结果?(Mr. Test Middlename User = TU)。”
    • @JakeScervino 好吧!!我的错。更新了答案。检查它现在是否有效
    • 我还要加strtoupper
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多