本文介绍了在php中将文本字符串拆分为$ first和$ last名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个私人消息系统,允许用户以其全名搜索用户,例如:乔治华盛顿".

I'm developing a private message system that allows users to search for a user by their full name, e.g.: "George Washington".

我有两个名为$firstname$lastname的变量,搜索功能按相关性对结果进行排序(您向该人发送消息的次数).如何获得一个文本字段,将乔治华盛顿"分为$firstname="George"$lastname="Washington"?

I have two variables named $firstname and $lastname, and the search function orders results by relevancy (how many times you have messaged that person). How do I get a text field to split "George Washington" into $firstname="George" and $lastname="Washington"?

推荐答案

我喜欢 cballou的答案,因为有人在努力检查是否只有名字.我以为我可以为其他来找我的人添加功能.

I like cballou's answer because there's an effort to check if there's only a first name. I thought I'd add my functions for anyone else who comes lookin'.

  • 它假设姓氏将是一个单词.
  • 不对中间名进行假设,所有中间名都被分组为名字.
  • 您可以在名字"结果上再次使用它来获得名字和中间名.

代码如下:

// uses regex that accepts any word character or hyphen in last name
function split_name($name) {
    $name = trim($name);
    $last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
    $first_name = trim( preg_replace('#'.$last_name.'#', '', $name ) );
    return array($first_name, $last_name);
}

示例1:split_name('Angeler')输出:

array(
    0 => 'Angeler',
    1 => ''
);

示例2:split_name('Angeler Mcgee')输出:

array(
    0 => 'Angeler',
    1 => 'Mcgee'
);

示例3:split_name('Angeler Sherlee Mcgee')输出:

array(
    0 => 'Angeler Sherlee',
    1 => 'Mcgee'
);

要分开名字和中间名,

示例4:split_name('Angeler Sherlee')输出:

array(
    0 => 'Angeler',
    1 => 'Sherlee'
);


另一个功能-也检测中间名

后来我决定最好自动找出中间名,所以我写了这个函数.


Another Function - Detects Middle Names Too

Later I decided that it would be nice to have the middle name figured out automatically, if applicable, so I wrote this function.

function split_name($name) {
    $parts = array();

    while ( strlen( trim($name)) > 0 ) {
        $name = trim($name);
        $string = preg_replace('#.*\s([\w-]*)$#', '$1', $name);
        $parts[] = $string;
        $name = trim( preg_replace('#'.$string.'#', '', $name ) );
    }

    if (empty($parts)) {
        return false;
    }

    $parts = array_reverse($parts);
    $name = array();
    $name['first_name'] = $parts[0];
    $name['middle_name'] = (isset($parts[2])) ? $parts[1] : '';
    $name['last_name'] = (isset($parts[2])) ? $parts[2] : ( isset($parts[1]) ? $parts[1] : '');

    return $name;
}

示例1:split_name('Angeler Sherlee Mcgee')输出:

array(
    'first_name' => 'Angeler',
    'middle_name' => 'Sherlee',
    'last_name' => 'Mcgee'
);

示例2:split_name('Angeler Mcgee')输出:

array(
    'first_name' => 'Angeler',
    'middle_name' => '',
    'last_name' => 'Mcgee'
);


另一种方式-Sans Regex

决定添加另一种不使用正则表达式的方式.


Another Way - Sans Regex

Decided to add another way that doesn't use regex.

对于无法识别的名称(空,空字符串,太多词组无法推断),它也具有return false;.

It also has return false; for non-recognizable names (null, empty string, too many word groups to infer).

<?php

function split_name($string) {
    $arr = explode(' ', $string);
    $num = count($arr);
    $first_name = $middle_name = $last_name = null;

    if ($num == 2) {
        list($first_name, $last_name) = $arr;
    } else {
        list($first_name, $middle_name, $last_name) = $arr;
    }

    return (empty($first_name) || $num > 3) ? false : compact(
        'first_name', 'middle_name', 'last_name'
    );
}

var_dump(split_name('Angela Mcgee'));
var_dump(split_name('Angela Bob Mcgee'));
var_dump(split_name('Angela'));
var_dump(split_name(''));
var_dump(split_name(null));
var_dump(split_name('Too Many Names In Here'));

输出

Array
(
    [first_name] => Angela
    [middle_name] => NULL
    [last_name] => Mcgee
)

Array
(
    [first_name] => Angela
    [middle_name] => Bob
    [last_name] => Mcgee
)

Array
(
    [first_name] => Angela
    [middle_name] => NULL
    [last_name] => NULL
)

false

false

false

这篇关于在php中将文本字符串拆分为$ first和$ last名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:14