Forum Moderators: coopster

Message Too Old, No Replies

ucfirst and explode not behaving as I'd expect them too

         

bomburmusicmallet

5:09 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



Hi everyone,

I have this code:


for ($i=0; $i<$count; $i++)
{
$topics = explode(";", $rowAR[$i]['topic']);
foreach ($topics as $topic)
{
$topicexpertAR[] = (array(ucfirst($topic),$rowAR[$i]['employeesID'],$rowAR[$i]['expert']));
}
}

The original text in the database is like this: "architectural history and theory; interior design history and theory; art history and theory", all lower-case unless a proper noun, and delimited by a semi-colon.

However, when I print from the array $topicexpertAR[], it ends up looking like this:


Topic ¦ Expert
Architectural history and theory ¦ Dr. Doctor
interior design history and theory ¦ Dr. Doctor
art history and theory ¦ Dr. Doctor

Only the first letter of the string $rowAR[$i]['topic'] is being changed to uppercase.

I have another record like this: "drug recalls by the FDA, etc., “pandemic” such as avian flu scare, west nile virus, etc." that ends up looking like this:


Topic ¦ Expert
drug recalls by the FDA, etc., “ ¦ Dr. Doctor
Pandemic” ¦ Dr. Doctor
such as avian flu scare, west nile virus, etc. ¦ Dr. Doctor

This entry doesn't even have semicolons.

Is there something wrong with my code? Thanks for looking.

ericjust

5:50 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



I'm not sure about the second part of your question.

The capitalization problem is due to whitespace at the beginning of the string. There is a space after the semi-colon and that is the first character in the string. Therefore ucfirst() is trying to capitalize a blank space.

You could use ucfirst(trim($topic)).

Hope this helps

bomburmusicmallet

7:55 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



Thank you! That was so obvious, but only in hindsight :)