- Split text string into $first and $last name in php
- Simple Function, Using Regex (word char and hyphens)
- Another Function — Detects Middle Names Too
- Another Way — Sans Regex
- Split text string into $first and $last name in php
- Php php validate first and last name
- PHP validation help. First and last name, validated as required fields
- Validating first names and last names in PHP to store in OpenLDAP
- Regex for First and Last Names
- Characters only Validation for Woocommerce First Name & Last Name
- Few things worth noting
- Laravel Livewire Crud with Bootstrap Modal Example
- How to Concat First Name and Last Name in PHP MySQL?
- You might also like.
Split text string into $first and $last name in php
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’.
Simple Function, Using Regex (word char and hyphens)
- It makes the assumption the last name will be a single word.
- Makes no assumption about middle names, that all just gets grouped into first name.
- You could use it again, on the «first name» result to get the first and middle though.
// uses regex that accepts any word character or hyphen in last name function split_name($name)
Ex 1: split_name(‘Angeler’) outputs:
Ex 2: split_name(‘Angeler Mcgee’) outputs:
array( 0 => 'Angeler', 1 => 'Mcgee' );
Ex 3: split_name(‘Angeler Sherlee Mcgee’) outputs:
array( 0 => 'Angeler Sherlee', 1 => 'Mcgee' );
To get the first and middle name split,
Ex 4: split_name(‘Angeler Sherlee’) outputs:
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('#'.preg_quote($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; >
Ex 1: split_name(‘Angeler Sherlee Mcgee’) outputs:
array( 'first_name' => 'Angeler', 'middle_name' => 'Sherlee', 'last_name' => 'Mcgee' );
Ex 2: split_name(‘Angeler Mcgee’) outputs:
array( 'first_name' => 'Angeler', 'middle_name' => '', 'last_name' => 'Mcgee' );
Another Way — Sans Regex
Decided to add another way that doesn’t use regex.
It also has return false; for non-recognizable names (null, empty string, too many word groups to infer).
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
The simplest way is, by using explode:
After you have the parts, pop the last one as $lastname :
Finally, implode back the rest of the array as your $firstname :
$firstname = implode(" ", $parts);
$name = "aaa bbb ccc ddd"; $parts = explode(" ", $name); if(count($parts) > 1) < $lastname = array_pop($parts); $firstname = implode(" ", $parts); >else < $firstname = $name; $lastname = " "; >echo "Lastname: $lastname\n"; echo "Firstname: $firstname\n";
tomatech:~ ariefbayu$ php ~/Documents/temp/test.php Lastname: ddd Firstname: aaa bbb ccc
Split text string into $first and $last name in php
In PHP, you can use the explode() function to split a string into an array, where each element of the array is a substring that was separated by a specified delimiter. To split a string into a first and last name, you can use a space » » as the delimiter. Here’s an example:
// Define a string with the full name $name = "John Smith"; // Split the full name into first and last name parts $name_parts = explode(" ", $name); // Assign the first name $first_name = $name_parts[0]; // Assign the last name $last_name = $name_parts[1]; // Output the first and last name echo "First name: " . $first_name . "\n"; echo "Last name: " . $last_name . "\n"; // Output: // First name: John // Last name: Smith ?>
In this example, the variable $name contains the full name «John Smith». The explode() function is used to split the string into an array, where each element of the array is a substring that was separated by a space. The first element of the array is assigned to the variable $first_name , and the second element is assigned to the variable $last_name .
If you expect the name to contain multiple parts like «John Doe Smith» or «John Wayne Smith Jr.» you can use the array_slice() function to get the last part of the array which will be the last name in most cases.
// Define a string with the full name $name = "John Smith"; // Split the full name into parts $name_parts = explode(" ", $name); // Extract the last name $last_name = array_slice($name_parts, -1)[0]; // Output the last name echo "Last name: " . $last_name . "\n"; // Output: // Last name: Smith ?>
You can also use preg_split() function which is regular expression version of explode function, which will help you split the string based on a regex pattern like » » (space) or «,» (comma) etc.
// Define a string with the full name $name = "John Smith"; // Split the full name into parts using a regular expression $name_parts = preg_split("/[\s,]+/", $name); // Assign the first name $first_name = $name_parts[0]; // Extract the last name $last_name = array_slice($name_parts, -1)[0]; // Output the first and last name echo "First name: " . $first_name . "\n"; echo "Last name: " . $last_name . "\n"; // Output: // First name: John // Last name: Smith ?>
This way you can split the text string into $first and $last name in PHP.
Php php validate first and last name
Syntaxes and Matching Rules Solution: Should do the trick Solution 1: I think you want to allow only alphabets in name fields in that case you can use ctype_alpha() Function Code goes in functions.php tested & works Solution 2: You can use the following code. Though there is a practical limit, LDAP clients should not assume that servers have the capability of enforcing the non-zero length of an attribute value.
PHP validation help. First and last name, validated as required fields
You can just use that using HTML 5, simple
This is a simple form
No need to use two big if s, just replace your PHP code with this :
"; echo "Click here to fix"; die; //if you mess up, youll have to fix it > else < echo " Your name is $name1 $name2"; >> ?>
First, whenever there is a die() statement,only the codes that appear before that run, the ones which come after don’t get to run(in relation to your code.)
you can also trim your codes down this way and it will still work
"; echo "Click here to fix"; die; //if you mess up, youll have to fix it > else < echo " Your name is $name1 $name2"; >> ?>
PHP validation help. First and last name, validated as, PHP validation help. First and last name, validated as required fields. Ask Question Asked 6 years, 5 months ago. Modified 6 years, 5 months ago. Once the submit button is hit, all data is sent and posted here. I can get first and last name to output fine, but if i leave them blank it will only say that i left my first … Code sampleThis is a simple form
Validating first names and last names in PHP to store in OpenLDAP
Validating names is a bad idea. There are no universal rules for what is allowed in a name, especially when you want to allow non-western text as well.
This presents a challenge when it comes to LDAP, since there is no guarantee that the directory will understand the charset of the user input. There is an easy solution to this though: base64_encode() the values before storing them in LDAP, and base64_decode() them on the way out.
There is no need to validate the names. The directory server schema specifies the syntax of all attributes, including givenName , and sn . The server will properly encode any names that are presented. If a DirectoryString value begins or ends with a space, the server will base64 encode the value for storage.
Though there is a practical limit, LDAP clients should not assume that servers have the capability of enforcing the non-zero length of an attribute value. Some attributes syntaxes are defined to be non-zero in length (such as DirectoryString ), but no upper limit is defined, therefore, clients should not assume the server enforces an upper limit.
see also
Php — Full Name Validation for first name, middle name, and when i open contact person information that phone already separating the full name into first middle and last name. like *first name : Dave middle name (first): Edward middle name (second) : James last name : David * there is the problem, how to do that on PHP validation? is it using implode / …
Regex for First and Last Names
Validation — Validating first names and last names in, There is no need to validate the names. The directory server schema specifies the syntax of all attributes, including givenName, and sn. The server will properly encode any names that are presented. If a DirectoryString value begins or ends with a space, the server will base64 encode the value for storage.
Characters only Validation for Woocommerce First Name & Last Name
I think you want to allow only alphabets in name fields in that case you can use ctype_alpha() Function
add_action( 'woocommerce_after_checkout_validation', 'validate_name', 10, 2); function validate_name( $fields, $errors )< $billing_first_name = $fields[ 'billing_first_name' ]; if ( !ctype_alpha($billing_first_name))< wc_add_notice(__('Only alphabets are alowed in Billing First Name.'), 'error'); > >
Code goes in functions.php tested & works
You can use the following code.
add_action( 'woocommerce_after_checkout_validation', 'bks_validate_fname_lname', 10, 2); function bks_validate_fname_lname( $fields, $errors )< if (!preg_match( '/^[a-zA-Z ]+$/', $fields[ 'billing_first_name' ]))< $errors->add( 'validation', 'First name should only have no special characters.' ); > if (!preg_match( '/^[a-zA-Z ]+$/', $fields[ 'billing_last_name' ]))< $errors->add( 'validation', 'Last name should only have no special characters.' ); > >
Few things worth noting
- As @CBroe said in the comment you should use + instead of * as it will check for empty submissions.
- You missed ! in your if condition.
- It’s better to inform the user exactly where is the issue, I have divided the code and checked for both of them seperately.
Regex — Last name validation in php, Fortunately, PHP regexes support Unicode classes, so this is easy to do. Unicode includes a class L for any letter (uppercase, lowercase, modified, and title case). This will allow accented letters in the name. I would also recommend that you allow for dashes (Katherine Zeta-Jones) and spaces (Guido van Rossum).
Laravel Livewire Crud with Bootstrap Modal Example
Hello Friends, In this blog, I would like to share with you how perform crud opeartion with bootstrap modal livewire in laravel application.I will.
How to Concat First Name and Last Name in PHP MySQL?
This article will give you example of how to concat first name and last name in PHP?. I explained simply about concat firstname and lastname in PHP. This tutorial will give you simple concat first name and last name in PHP.
In this post, You’ll learn PHP query to concat first name and last name. I will show you use of concat first and last name PHP with space.
Here i will give you many example first name and last name concatenate in PHP.
Table: users
So, let’s see bellow solution:
query($sql); while ($row = $result->fetch_assoc()) < echo ''; print_r($row); > ?>Array ( [id] => 1 [full_name] => Hardik Savani [email] => savanihd@gmail.com [created_at] => 2020-09-12 06:46:08.000000 ) Array ( [id] => 2 [full_name] => Aatman Infotech [email] => aatmaninfotech@gmail.com [created_at] => 2020-09-30 13:33:52.000000 ) Array ( [id] => 3 [full_name] => Harshad Pathak [email] => savanhd2@gmail.com [created_at] => 2020-09-12 06:46:08.000000 )✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.
You might also like.