Last Updated: February 25, 2016
·
632
· searsaw

Splitting up names

Sometimes you only have one text box for a user's whole name (first and last). Normally, though, we want to store it as first name and last name in a database for search purposes. Here is a quick way to split it up and to check that it was enter correctly at the same time.

if (!strpos($_POST['full_name'], ' ') return false;

$names = explode(' ', $_POST['full_name'], 2);
$first_name = $names[0];
$last_name = $names[1];

This will essentially split some input on the first blank space. The array it will return will hold the first name and then everything else. So if they entered the middle name with it, then the $names[1] will hold the middle and last name.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside