JSHINER Posted May 7, 2007 Share Posted May 7, 2007 I currently use the following to pull last name: SELECT"vendors.name, SUBSTRING_INDEX(vendors.name, ' ', -1) AS lastname FROM table I did this so I could sort by last name. But now I want it to display last name FIRST, then the rest. So I would like to create another variable... firstname - so I can display John A. Smith as Smith, John A. How can I do this ? Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/ Share on other sites More sharing options...
papaface Posted May 7, 2007 Share Posted May 7, 2007 explode(".",$string); Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247338 Share on other sites More sharing options...
JSHINER Posted May 7, 2007 Author Share Posted May 7, 2007 That uses the . as the end point. I need to use spaces. I need the last space John A _ Smith as the divider. Everything before = first name, everything after = last name. The code I posted uses everything after the last space as last name. I need something similar for first name. The reason is some names are "Mike James Smith" And do not use middle initials, but full names. Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247340 Share on other sites More sharing options...
papaface Posted May 7, 2007 Share Posted May 7, 2007 huh? <?php $string = "John A. Smith"; $explode = explode(".",$string); echo $explode[1] . ", " . $explode[0]; //produces Smith, John A ?> Its almost impossible to make something to work with every kind of name. I suggest you try to make sure all names are entered in the same way. Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247343 Share on other sites More sharing options...
craygo Posted May 7, 2007 Share Posted May 7, 2007 if the name for the vendor looks like this John A Smith or John Allen Smith you could always do this $names = explode(" ", $lastname); $firstname = $names[0]; $middlename = $names[1]; $lastname = $names[2]; This will break up the name into parts separated by spaces. Ray Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247344 Share on other sites More sharing options...
JSHINER Posted May 7, 2007 Author Share Posted May 7, 2007 Is there a way to make this work so that if there isn't a middle name, it still displays correctly? Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247376 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2007 Share Posted May 7, 2007 This should give you what you're looking for: $names = array('Joe A. Smith','Joseph Able Smith','Joe Smith'); foreach ($names as $name) { $lp = strrpos($name,' '); // get the position of the last space in the string echo trim(substr($name,$lp)) . ', ' . trim(substr($name,0,$lp)) . "<br>"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247388 Share on other sites More sharing options...
obsidian Posted May 7, 2007 Share Posted May 7, 2007 Try something like this: <?php function parseName($string) { if (preg_match('|(.+) ([a-z\-\']+)\z|i', $string, $match)) { $lname = $match[2]; $fname = $match[1]; $string = "$lname, $fname"; } return $string; } $names = array('John Smith', 'John A. Smith', 'J. Smith'); $names = array_map('parseName', $names); print_r($names); ?> Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247412 Share on other sites More sharing options...
neel_basu Posted May 7, 2007 Share Posted May 7, 2007 You can use this function ----------------------------- <?php function rev_name($str, $sep=' ') { $r = explode($sep, $str); $txt = ''; for($i = count($r)-1;$i>=0;$i--) { $txt .= $r[$i]; if($i != 0) { $txt .= $sep; } } unset($r); return $txt; } ?> Use it in this way echo rev_name('start mid last'); It would output last mid start [sorry cant use [ code ] Here] Is this what you have wanted ?? or something else. Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247434 Share on other sites More sharing options...
neel_basu Posted May 7, 2007 Share Posted May 7, 2007 This is another function perhaps this is what you need ---------------------------------------------------- <?php function rev_name($str, $sep=' ') { $r = explode($sep, $str); if(count($r) > 2) { $txt = ''; for($i = count($r)-2;$i>=1;$i--) { $txt .= $r[$i]; if($i != 0) { $txt .= $sep; } } return $r[count($r)-1].$sep.$r[0].$sep.$txt; } else { return $r[count($r)-1].$sep.$r[0]; } } echo rev_name('start mid last'); ?> Output ----------------------------- last start mid If you use rev_name('start last'); it will output is last start ===================================================== || But none of those functions are Tested at all. || ===================================================== Link to comment https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/#findComment-247447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.