Jump to content

Correct capitalization: McDonald III etc.


Borderline

Recommended Posts

I'm trying to produce a result whereby horse & jockeys names are capitalized correctly. My current results are located here: http://www.further-f...t/testupper.php McNaughton & Billie-Jo are showing correctly, but McDonald and O'Connor aren't. Additionally, I'd like to attempt to have Ii displayed as II. My code is below - could it be tweaked to correct these issues, or is there a better way of achieving all this anyway? Any info gratefully received.

<?PHP	function FormatName($name=NULL) {	  		if (empty($name))			return false;		$name = strtolower($name);		$names_array = explode('-',$name);		for ($i = 0; $i < count($names_array); $i++) {		  			if (strncmp($names_array[$i],'mc',2) == 0 || ereg('^[oO]\'[a-zA-Z]',$names_array[$i])) {			$names_array[$i][2] = strtoupper($names_array[$i][2]);  			}		  			$names_array[$i] = ucfirst($names_array[$i]);		  		}		$name = implode('-',$names_array);		return ucwords($name);	  	}    // Get all the data from the table	$result = mysql_query("SELECT horse, jockey FROM testnames") or die(mysql_error());	echo "<table class='correctenglish' border='1' cellpadding='4' cellspacing='0' width='50%'>";	echo "<tr class='toprow'><th>Horse</th> <th>Jockey</th></tr>";  	// keeps getting the next row until there are no more to get  	while($row = mysql_fetch_array( $result )) {  	// Print out the contents of each row into a table	  echo  "<tr>";	  echo  "<td align='center'>";	  echo  FormatName($row['horse']);	  echo  "</td>";	  echo  "<td align='center'>";	  echo  FormatName($row['jockey']);	  echo  "</td>";		  echo  "</tr>";	  }	  echo  "</table>";?>

Link to comment
Share on other sites

Here's what I use for that:

function uc_names($s){  return preg_replace_callback("/(\b[\w|']+\B)/s", 'fixcase_callback', $s);}function fixcase_callback($word){  $word = $word[1];  $word = strtolower($word);  if($word == "de")	return $word;  $word = ucfirst($word);  if(substr($word,1,1) == "'")  {	if(substr($word,0,1) == "D")	{	  $word = strtolower($word);	}	$next = substr($word,2,1);	$next = strtoupper($next);	$word = substr_replace($word, $next, 2, 1);  }  return $word;}

http://www.php.net/m...ce-callback.php

  • Like 1
Link to comment
Share on other sites

Thanks for the reply. Doing that gets me a strange result: http://www.further-flight.co.uk/site/test/justsomeguy.php

function uc_names($s){  return preg_replace_callback("/(\b[\w|']+\B)/s", 'fixcase_callback', $s);  //return $s;}function fixcase_callback($word){  $word = $word ;  $word = strtolower($word);  if($word == "de")    return $word;  $word = ucfirst($word);  if(substr($word,1,1) == "'")  {    if(substr($word,0,1) == "D")    {   $word = strtolower($word);    }    $next = substr($word,2,1);    $next = strtoupper($next);    $word = substr_replace($word, $next, 2, 1);  }  return $word;}   // Get all the data from the table    $result = mysql_query("SELECT horse, jockey FROM testnames") or die(mysql_error());     echo "<table class='correctenglish' border='1' cellpadding='4' cellspacing='0' width='50%'>";    echo "<tr class='toprow'><th>Horse</th> <th>Jockey</th></tr>";       // keeps getting the next row until there are no more to get       while($row = mysql_fetch_array( $result )) {       // Print out the contents of each row into a table	  echo  "<tr>";	  echo  "<td align='center'>";	  echo  uc_names($row['horse']);	  echo  "</td>";	  echo  "<td align='center'>";	  echo  uc_names($row['jockey']);	  echo  "</td>";	 	  echo  "</tr>";	  }	  echo  "</table>";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...