ucfirst Not Working On explode() Query

I need to figure out how to take string “$items”, explode it into parts, and capitalize the first letter of each individual result, instead of just the first letter of the original string. Thanks in advance!

[php]//this falls under category $category_name

if($item_list) {
//get list and make radio options
$items = explode(’,’,$item_list); ?>


Choose one:


<?php
//PROBLEM BEGINS HERE, I THINK
foreach($items as $option) { ?>
<input type=“radio” name"<?php echo $category_name.'_optn'; ?>" value="<?php echo $option; ?>" id="<?php echo "sub_".$option; ?>">
<?php echo ucfirst($option); ?>
<?php /*--------
the above ucfirst($option) returns each option individually as intended, but ucfirst only applies to the first letter of the original string $items */
}//end foreach
?>


<?php } //end if ?>[/php]

If figured it out, sort of. I was able to create the intended result by creating an array with the original result, so that each result stores its own increment-x:
[php]//this falls under category $category_name

if($item_list) {
//get list and make radio options
$items = explode(’,’,$item_list); ?>


Choose one:


<?php
//separate items
foreach($items as $option) {
/* ----------- NEW CODE --------------- /
//give separate items their own identity
$si = is_array($option)?$option:trim($option);
//now give ucfirst
$good_optn = ucfirst($si);
/
------------- NOW it’s ready to play nicely below ------------*/
?>
<input type=“radio” name"<?php echo $category_name.'_optn'; ?>" value="<?php echo $option; ?>" id="<?php echo "sub_".$option; ?>">
<?php echo $good_optn; ?>
<?php
}//end foreach
?>


<?php } //end if ?>[/php]

your solusion doesnt seem to make any difrence, unless you removed some code:
$option can’t ever be an array as it is an element of explode
so $good_optn is just ucfirst(trim($option)).
therefore the only diffrence is trim()

to me it still sounds like u only need ucwords()

but i’d like to know the data in $item_list

Damn! Q1712 was right. It had nothing to do with whether or not it was an array, as was what my initial thoughts were.

trim() seemed to fix the problem itself. I think the reason it was only working on the first word of the entire $item_list string (ie. the very first $option only) was because the first character of each ensuing string was a space. So, trimming off the whitespace took care of that without any more coding needed.

More on the subject:
$item_list is a list made up of 3 or more words, separated by a comma. For example: “Wadded up paper”, “lizards on a log”, etc.

The idea was to make only the first letter (not the first letter of each word) of each string ($option) capital, so as to read uniformly without being overwrought with capitals - so not ucwords().

Thanks!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service