Jump to content

First, middle, and last names


ejaboneta

Recommended Posts

I'm trying to set Apache to forward urls that would include a persons first name and last name. I would then use the first and last name to grab the content from a database

 

example:

http://example.com/profiles/michael-jackson

forwards to

http://example.com/profiles.php?first=michael&last=jackson

 

I've got it working using

profiles/([a-zA-Z]*)(-*)([a-zA-Z]*)

 

from this I take the first part as the first name and the third part as the last name since the second part is "-".

 

I'm having problems with middle names and names with hypens. Like if the name was Neil Patrick Harris, it would become Neil-Patrick-Harris and my code would take "Neil" as the first name and "Patrick" as the last name.

 

I thought since I'd be using php to grab the actual content, later I could just grab the last word and use that as the last name but then there'd be the problem with hyphenated names. "John Doe-Smith" would become "John-Doe-Smith" and only "Smith" would be taken as the last name.

 

 

 

Any help on how I can do this? I figured the solution would be a regex one since thats how I'm formatting all this.

Link to comment
https://forums.phpfreaks.com/topic/169895-first-middle-and-last-names/
Share on other sites

To overcome the problem with hyphenated names, you could use underscores in the URL instead of hyphens. So try this:

 

profiles/([a-zA-Z-]+)[a-zA-Z_-]*_([a-zA-Z-]+)

 

It should grab the first and last name, even when there's more than two names. It also allows hyphens in the names. Just remember to use underscores in the URL;

 

http://example.com/profiles/john_doe-smith_junior

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.