Identifying the first and last element in an Array

7 posts by 2 authors in: Forums > CMS Builder
Last Post: November 19, 2012   (RSS)

Hi All,

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
The first CMS Builder reference book is now available on-line!







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

Hi Jerry,

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.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Identifying the first and last element in an Array

Hi Dave,

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
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

labels-spread4.php 15K

Re: [Dave] Identifying the first and last element in an Array

By gkornbluth - November 17, 2012 - edited: November 17, 2012

Hi Dave,

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
The first CMS Builder reference book is now available on-line!







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

Hi Jerry,

>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!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Identifying the first and last element in an Array

By gkornbluth - November 19, 2012 - edited: November 20, 2012

Hi Dave,

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
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php