Jump to content

[SOLVED] capitalization works partially... and then...


zeddeh

Recommended Posts

Hello all,

 

I am trying to fix a capitalization issue since some of the database last names are non-standard: MacDonald, or van Berg, Wolfe-Milner, etc.

 

I have got the Mc... and the Mac... working ok and made a line for Jr. (see code below) but then when I put in another section to change the code for Wolfe-Milner the Mc and Mac's now revert to lower case and only the Wolfe-Milner section works. It seems to only recognize the last bit of code and ignores the first ones.

 

I am perplexed as to what is in the Wolfe-Milner code that is causing the first 2 sections to be ignored.

 

Any ideas?

 

Many thanks!

 

 

-------- start code -------

//Fix internal capitalization

$lastName_temp = strtolower($data_array[$i][1]);

$pos = strpos($lastName_temp,"mc");

if($pos === 0) {

$lastName_temp = substr($lastName_temp,2);

$lastName_temp = "Mc".ucfirst($lastName_temp);

}else{

$lastName_temp = ucfirst(strtolower($data_array[$i][1]));

}

 

$lastName_temp = strtolower($data_array[$i][1]);

$pos = strpos($lastName_temp,"mac");

if($pos === 0) {

$lastName_temp = substr($lastName_temp,3);

$lastName_temp = "Mac".ucfirst($lastName_temp);

}else{

$lastName_temp = ucfirst(strtolower($data_array[$i][1]));

}

 

$lastName_temp = strtolower($data_array[$i][1]);

$pos = strpos($lastName_temp,"wolfe-");

if($pos === 0) {

$lastName_temp = substr($lastName_temp,6);

$lastName_temp = "Wolfe-".ucfirst($lastName_temp);

}else{

$lastName_temp = ucfirst(strtolower($data_array[$i][1]));

}

 

$lastName_temp = str_ireplace(" jr"," Jr",$lastName_temp);

Before each check you re-read the variable from its original source, thus losing the changes you made before.

$lastName_temp = strtolower($data_array[$i][1]);

 

This might be easier for you:

<pre>
<?php
$tests = array(
	'mcdonald',
	'macdonald',
	'ronald mcdonald',
	'ronald macdonald',
	'bob wolfe-johnson',
	'howard harrison, jr.',
	'howard harrison, jr',
);
foreach ($tests as $test) {
	echo $test, ' => ';
	$test = preg_replace('/\b((?:ma?c)|(?:wolfe-)|(?:jr\.?\b))/e', 'ucfirst("\1")', $test);
	echo $test, '<br>';
}
?>
</pre>

Hmmm, yes I figured I might be doing something like that. Welcome to my world of the inexperienced.

 

Your code sure is a lot more compact, I will give it a try and also try to look at it more deeply to see how it works as I will need to edit it to include more names also.

 

Many thanks

 

Zeddeh

Hmmm, yes I figured I might be doing something like that. Welcome to my world of the inexperienced.

 

Your code sure is a lot more compact, I will give it a try and also try to look at it more deeply to see how it works as I will need to edit it to include more names also.

 

Many thanks

 

Zeddeh

 

In that case you'll want to look at http://www.regexp.info. effigy's code relies heavily on a regular expression (which is a good thing), so without understanding it you can't add more on.

Archived

This topic is now archived and is closed to further replies.

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