Sign up ×

I have a SAGE200 database full of product names that, in most cases, have been entered in all capital letters. These are being displayed on our website as entered, which is unsightly. I am new to PHP, but discovered the ucwords function and employed that, along with strtolower so that I end up with Title Case for the items being displayed. This is better, but still far from ideal as some products need some uppercase letters or words in them, as this is their correct naming convention.

Is there some way I can code this so that Title Case is applied to my product names, as well as there being a lookup against a list I define somewhere to leave some words as all caps?

This is all in Magento

Perhaps a translation file?

I am really not sure of the best way to approach this, and would appreciate any pointers.


share|improve this question
I don't have a Mage install available off hand, but your best course would probably be to create a helper and use it on the product name in the model - if I can get an install going, I'll definitely post some code. –  Nic Aug 31 '11 at 16:51
That would be superb. I don't yet know how to do this sort of thing (not enough time spent as yet!) - thanks! –  robgt Sep 1 '11 at 8:25

2 Answers 2

If you already know the translations (I assume so, since you suggested a translation file), why not just convert the product names in your database?

share|improve this answer

One option is to leave letters you certainly want to be uppercase as uppercase and only have your ucwords function uppercase the beginning of words. You wouldn't use ucwords. You would use:

$string = preg_replace('/(^|[^a-zA-Z])[a-z]/e', 'strtoupper("$0")', $string);

That will uppercase any letter that is not following a letter.

Otherwise, if you have a list of words you want to uppercase, I would do that before using ucwords because ucwords never lowercases letters. It would look like:

$string = strtolower($string);
foreach($mywordlist as $word)
    $string = str_replace(strtolower($word), strtoupper($word), $string);
$string = ucwords($string);

A note: the preg_replace I used above is what I use instead of ucwords all the time because it uppercases words where there are non-space characters. Example: ucwords turns tick-tock into Tick-tock. My preg_replace turns it into Tick-Tock.

share|improve this answer

Your Answer


