knox203 Posted September 8, 2008 Share Posted September 8, 2008 Hello all, I'm trying to separate names in a list. I'm having a hard time telling the code to do the same thing if the name format is either "John M. Doe" or "John Middle Doe". Here's the code: <?php $odbc = odbc_connect('sms','','') or die(odbc_errormsg()); $strsql= 'SELECT * FROM cardmstr.DBF'; $query = odbc_exec($odbc, $strsql) or die (odbc_errormsg()); while($row = odbc_fetch_array($query)) { if ( ereg("^([A-Za-z]+ ?[A-Za-z]?.?) ([A-Za-z]+)", $row['item_desc'], $new) ) { echo '<b>'.$new[1]." ".$new[2].'</b><br />'."\n"; } } odbc_close($odbc); ?> Results returned with this code looks like: "John M. Doe" "John Middle" I tried <?php ereg("^([A-Za-z]+ ?([A-Za-z]|[A-Za-z]+)?.?) ([A-Za-z]+)", $row['item_desc'], $new) ?> but fails all together. Any sort of help would be great, thanks! - Adam Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/ Share on other sites More sharing options...
effigy Posted September 8, 2008 Share Posted September 8, 2008 Are the names together in one field and you're trying to separate the names, or are you trying to break a single name into pieces? Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-636846 Share on other sites More sharing options...
knox203 Posted September 8, 2008 Author Share Posted September 8, 2008 Hey effigy, I'm trying to break the names into pieces. Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-636965 Share on other sites More sharing options...
effigy Posted September 8, 2008 Share Posted September 8, 2008 How about this? $pieces = preg_split('/\S+/', $name); Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-636975 Share on other sites More sharing options...
nrg_alpha Posted September 8, 2008 Share Posted September 8, 2008 $pieces = preg_split('/\S+/', $name); Effigy.. I'm rather curious.. As I examine your pattern, I can't help but wonder.. Since you are matching any non-space character one or more consecutive times.. is this pretty much a 'beefier' version of: '/\b\w+\b/' ? (since \w is basically a-zA-Z0-9_ (locale issues aside), I suppose your pattern anticipates a broader range of characters and let's the spaces act as 'word bounderies' or sorts) Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-637001 Share on other sites More sharing options...
knox203 Posted September 8, 2008 Author Share Posted September 8, 2008 You know, I'm not too sure, I'm new to regex. I need to group "John M." and "John Middle" as one group (first name) and "Doe" as last name. The names are in all sorts of different formats, so I'll include the rest of the code to hopefully give you a feel for what I'm working with. <?php $odbc = odbc_connect('sms','','') or die(odbc_errormsg()); $strsql= 'SELECT * FROM cardmstr.DBF'; $query = odbc_exec($odbc, $strsql) or die (odbc_errormsg()); while($row = odbc_fetch_array($query)) { // LastName, FirstName if ( ereg("^([A-Za-z]+)(,) ([A-Za-z]+) (J(unio)?r(.)?|S(enio)?r(.)?)?", $row['item_desc'], $new) ) { echo '<b>'.$new[3]." ".$new[1]." ".$new[4].'</b><br />'."\n"; } // FirstName (2nd FirstName) LastName - Emp# elseif ( ereg("^([A-Za-z]+( *)?([A-Za-z]+)?) ([A-Za-z]+)( *)?- (C?[0-9]+)", $row['item_desc'], $new) ) { echo '<b>'.$new[1]." ".$new[4].'</b><br />'."\n"; } // Emp# - FirstName LastName elseif ( ereg("^(C?[0-9]+) (-)? ?([A-Za-z.?]+) ([A-Za-z]+)", $row['item_desc'], $new) ) { echo '<b>'.$new[3]." ".$new[4].'</b><br />'."\n"; } // "Truck #0-9" - Vehicle Type elseif ( ereg("^Truck (#[1-9]+) - ([A-Za-z]+)", $row['item_desc'], $new) ) { echo '<b>'.$row['item_desc'].'</b><br />'."\n"; } // FirstName LastName elseif ( ereg("^([A-Za-z]+ ?[A-Za-z]?.?) ([A-Za-z]+)", $row['item_desc'], $new) ) { echo '<b>'.$new[1]." ".$new[2].'</b><br />'."\n"; } elseif ( trim($row['item_desc']) ) { echo '<b>'.$row['item_desc'].'</b><br />'."\n"; } } odbc_close($odbc); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-637008 Share on other sites More sharing options...
DarkWater Posted September 8, 2008 Share Posted September 8, 2008 I think Effigy's solution is relatively optimal, if you're basically splitting by spaces anyway (that's what you're really doing). Also, don't use ereg(). Use preg_* functions because ereg() and the POSIX extended regular expressions will be removed from PHP 6 (only to be available by means of PECL). Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-637028 Share on other sites More sharing options...
effigy Posted September 9, 2008 Share Posted September 9, 2008 $pieces = preg_split('/\S+/', $name); Effigy.. I'm rather curious.. As I examine your pattern, I can't help but wonder.. Since you are matching any non-space character one or more consecutive times.. is this pretty much a 'beefier' version of: '/\b\w+\b/' ? (since \w is basically a-zA-Z0-9_ (locale issues aside), I suppose your pattern anticipates a broader range of characters and let's the spaces act as 'word bounderies' or sorts) Essentially, yes. In many cases preg_split is better because you're being specific about what you do not want, rather than attempting to make a pattern that's versatile enough to match what you do want. The names are in all sorts of different formats What problems are you having with the given code? Do you need each variation to look for a middle name? Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-637502 Share on other sites More sharing options...
knox203 Posted September 9, 2008 Author Share Posted September 9, 2008 Thanks for your advice everyone. I'll see what I can come up with. (PREG looks like it's the way to go) Quote Link to comment https://forums.phpfreaks.com/topic/123311-solved-regex-condition-not-working/#findComment-637716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.