Laravel 5.2 Split String名字姓氏 [英] Laravel 5.2 Split String First Name Last Name

查看:43
本文介绍了Laravel 5.2 Split String名字姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从全名形式传递的字符串.

I have a string passed from a form which is full name.

在我的数据库中,我存储了名字和姓氏.我使用以下命令拆分了字符串:

in my database I store first name and last name.. I've split the string using the following:

$name = explode(" ", $request->name);
$lastname = array_pop($name);
$firstname = implode(" ", $name);

这很好用,但是,如果用户未在该字段中输入姓氏,则上述功能将不起作用,因为姓氏将成为姓氏.

this works great, however, if the user doesn't enter a surname in the field then the above doesn't work as the lastname becomes the first.

我想念什么吗?

推荐答案

这是我用来拆分名称的方法:

This is what I've used for splitting names:

$splitName = explode(' ', $name, 2); // Restricts it to only 2 values, for names like Billy Bob Jones

$first_name = $splitName[0];
$last_name = !empty($splitName[1]) ? $splitName[1] : ''; // If last name doesn't exist, make it empty

这篇关于Laravel 5.2 Split String名字姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆