开发者

UCWords function tweaks

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 Titl开发者_JAVA百科e 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 1.4.0.1.

Perhaps a translation file?

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

Cheers!


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜