If a form field collects a name into a variable, such as $name = “Fred Smith”, is there a function in PHP—or some easy means—to extract just the first name?
In other words, I’d like to end up with a new variable like $first-name = “Fred”.
If someone has two first names this won’t work of course.
You might be tempted to take every word up until the last space, but that doesn’t work if someone has multiple last names.
This is exactly the reason why I always ask for first name and last name seperately in forms
Thanks guido. Why do you say it wouldn’t work with those names? I would be happy with extracting “john” and “Jane”, so it looks like it would work for me.
Yes, I realize there is a bit of luck involved with this (it would be funny if someone wrote Mr or Mrs before the name!) but I figure it’s reasonably safe in general. I often collect first and last names separately, but on a current form the client would prefer the whole name in one, so I thought I’d try out this method.