Identifying the first and last element in an Array
7 posts by 2 authors in: Forums > CMS Builder
Last Post: November 19, 2012 (RSS)
I'm using the function below to re-format the titles in my viewer to fit standard grammatical conventions.
It forces Roman Numerals to be all caps and forces specific words to be lower case, while capitalizing all other words in the title.
I’ve added the code in red in an attempt to always keep the first and last words in the title ($instruction) capitalized, even if the word is in the $lower_exceptions list.
It seems to work for the first word of the title, but not for the last word, and I can't figure out why.
<?php
function RemoveShouting($instruction)
{
$lower_exceptions = array(
"to" => "1", "a" => "1", "the" => "1", "and" => "1", "but" => "1", "or" => "1", "for" => "1", "nor" => "1", "of" => "1"
);
$higher_exceptions = array(
"I" => "1", "II" => "1", "III" => "1", "IV" => "1",
"V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1",
"IX" => "1", "X" => "1"
);
$words = split(" ", $instruction);
$newwords = array();
foreach ($words as $word)
{
$firstElement = reset($newwords);
$lastElement = end($newwords);
if (!$higher_exceptions[$word])
$word = strtolower($word);
if (!$lower_exceptions[$word] || !$word == $firstElement || !$word == $lastElement )
$word = ucfirst($word);
array_push($newwords, $word);
}
return join(" ", $newwords);
}
?>
And to output the re-formatted code for $instruction:
<?php echo RemoveShouting($instruction) ?>
I hope someone with more understanding of this has a suggestion.
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Identifying the first and last element in an Array
By Dave - November 17, 2012
It might be related to Operator Precedence:
http://php.net/manual/en/language.operators.precedence.php
I'm a big fan of adding extra brackets, try this:
if (!$lower_exceptions[$word] || !($word == $firstElement) || !($word == $lastElement))
I haven't looked at the code too closely, but that's the first step to try. Let me know if that works for you.
interactivetools.com
Re: [Dave] Identifying the first and last element in an Array
Don't you ever take weekends off?
Seriously, thanks for looking at this.
I had to put the ! inside the brackets or things got worse and interior $lower_exception words were again capitalized.
Even with the ! inside the brackets, when I add one of the $lower_exception words to the end of the Title that word remains lower case.
I've attached the entire file just in case. The function starts on line 186 and the first insertion is on line 279.
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [Dave] Identifying the first and last element in an Array
By gkornbluth - November 17, 2012 - edited: November 17, 2012
As long as you’re working, I’ve got another hopefully quick question...
I found this to capitalize both parts of a hyphenated name:
<?php function ucwordsHyphen($last_name){
return str_replace('- ','-',ucwords(str_replace('-','- ',$last_name)));
}
?>
Then in the foreach loop,
<?php $last_name = ucwordsHyphen(strtolower($record['last_name'])) ; ?>
<?php echo $last_name ?>
Don't think so, but have I created a security risk by using this?
Any better suggestion?
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Identifying the first and last element in an Array
By Dave - November 18, 2012
>I found this to capitalize both parts of a hyphenated name:
>Don't think so, but have I created a security risk by using this?
Because it's only modifying text you're safe. Security risks come into play when someone can modify what is inserted into a database, and that input isn't escaped properly, or something like that.
Keep in mind, though, that capitalizing names (and related tasks like splitting them into first/last names) is one of the most difficult tasks in programming. It's easy to get it 98% right, but nearly impossible to get it 100% right. Although often 98% is enough depending on the use.
Hope that helps!
interactivetools.com
Re: [gkornbluth] Identifying the first and last element in an Array
By Dave - November 18, 2012
Putting a ! in front of a variable will convert that variable to a true or false value and probably not do what you expect. Here's an example:
$word = "Cat";
print "<p>\$word == Cat: "; // prints: $word == Cat: True
if ($word == "Cat") { print "True"; } else { print "False"; }
print "<p>\$word == Dog: "; // prints: $word == Dog: False
if ($word == "Dog") { print "True"; } else { print "False"; }
print "<p>!\$word == Cat: "; // prints: !$word == Cat: False
if (!$word == "Cat") { print "True"; } else { print "False"; }
print "<p>!\$word == Dog: "; // prints: !$word == Dog: False
if (!$word == "Dog") { print "True"; } else { print "False"; }
For the second two it's actually just comparing if false == whatever value. One easy way around that is just to use the != operator (not equals).
Next, for debugging the code that identifies the first and last word, the best way to debug this (or any issue) is to recreate the problem in the most minimal way on a test page. I have a web server running on my local machine (I use wampserver.com) and keep a file called test.php for this purpose that I can access at any time at: http://localhost/test.php.
Here's some code to reproduce the first/last issue:
$instruction = "The quick brown fox jumps over the lazy dog.";
$words = split(" ", $instruction);
foreach ($words as $word) {
$isFirst = ($word == reset($words));
$isLast = ($word == end($words));
print "Word: '$word'";
if ($isFirst) { print " (is first)"; }
if ($isLast) { print " (is last)"; }
print "<br/>\n";
}
// Outputs:
In writing the code (which now works) I saw that you had reset() and end() checking $newwords, and I think what you want is to have it checking $words.
Also, I recommend always using curly braces after if statements, you don't technically have to, but it just makes it more clear and prevents errors and unexpected results. eg:
if (!$higher_exceptions[$word]) { $word = strtolower($word); }
I always try to make my code as clear and explicit as possible, leaving no room for confusion or doubt, by either the computer, another programmer, or myself when I come back to look at my code 6 months later and wonder what I was thinking... :)
Hope that helps!
interactivetools.com
Re: [Dave] Identifying the first and last element in an Array
By gkornbluth - November 19, 2012 - edited: November 20, 2012
Thanks for spending so much time on this, and for your detailed explanations.
After incorporating your suggestions and mucking about, I came up with something that seems to work without breaking anything else (although I'm still not exactly sure why).
Here's the function that resulted:
<?php //Title formatting works on both first and last word.
function RemoveShouting($instruction)
{
$lower_exceptions = array(
"to" => "1", "a" => "1", "the" => "1", "and" => "1", "but" => "1", "or" => "1", "for" => "1", "nor" => "1", "of" => "1"
);
$higher_exceptions = array(
"I" => "1", "II" => "1", "III" => "1", "IV" => "1",
"V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1",
"IX" => "1", "X" => "1"
);
$words = split(" ", $instruction);
$newwords = array();
foreach ($words as $word)
{
$firstElement = ($word == reset($words));
$lastElement = ($word == end($words));
if (!$higher_exceptions[$word]) {$word = strtolower($word);}
if ((!$lower_exceptions[$word]) || ($word == $firstElement) || ($word == $lastElement) ) {$word = ucfirst($word);}
array_push($newwords, $word);
}
return join(" ", $newwords);
}
?>
As always, I thank you.
I don't think I could do these things without your expert help and guidance.
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php