curtm Posted December 5, 2007 Share Posted December 5, 2007 I have some emails that I'm parsing into a database. In those emails, there are names. the names have no controls on the way they are typed in, so I have data like so... fname m lname fname m. lname fname m lname suffix fname mname lname fname lname fname lname suffix How can I parse this so I get the right data in the right field? I have fname m lname for my database fields. thanks! Link to comment https://forums.phpfreaks.com/topic/80315-solved-parseing-names/ Share on other sites More sharing options...
wildteen88 Posted December 5, 2007 Share Posted December 5, 2007 If the names themselves don't have have space within them then you could use explode to split the names into three variables, like so: $name = 'fname m lname'; list($fname, $mname, $lname) = explode(' ', $name); echo 'Firstname: ' . $fname . '<br />'; echo 'Middlename: ' . $mname . '<br />'; echo 'Surname: ' . $lname; Link to comment https://forums.phpfreaks.com/topic/80315-solved-parseing-names/#findComment-407045 Share on other sites More sharing options...
curtm Posted December 5, 2007 Author Share Posted December 5, 2007 I'm using the EXPLODE function now, but that doesn't help me get the names in the right fields. If one guy puts fname lname and the next guy puts fname m lname, the explode would have to have conditions for the names to go in the right field. Link to comment https://forums.phpfreaks.com/topic/80315-solved-parseing-names/#findComment-407093 Share on other sites More sharing options...
wildteen88 Posted December 6, 2007 Share Posted December 6, 2007 You could check to see how many parts have been returned from explode, if its two then fname lname if three fname m lname. $name = 'fname lname'; $parts = explode(' ', $name); switch(count($parts)) { case 2: list($fname, $lname) = explode(' ', $name); $mname = 'N/A'; break; case 3: list($fname, $mname, $lname) = explode(' ', $name); break; } echo 'Firstname: ' . $fname . '<br />'; echo 'Middlename: ' . $mname . '<br />'; echo 'Surname: ' . $lname; Link to comment https://forums.phpfreaks.com/topic/80315-solved-parseing-names/#findComment-408177 Share on other sites More sharing options...
curtm Posted December 6, 2007 Author Share Posted December 6, 2007 Thanks! Link to comment https://forums.phpfreaks.com/topic/80315-solved-parseing-names/#findComment-408220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.