Jump to content

[SOLVED] Regex condition not working


knox203

Recommended Posts

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

Link to comment
Share on other sites

$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)

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

$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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.