News:

Wondering if this will always be free?  See why free is better.

Main Menu

Title Case in Subject

Started by cosmicxxx, September 28, 2011, 12:38:20 PM

Previous topic - Next topic

cosmicxxx

hello gurus,

is there any mod, or how-to's on how to do this? im thinking of a mod that can automatically transform the subject's title, into title case.

so, if a member inputs lowercase in Subject, in the post topic page, and when submitting this topic post, the subject title will then be automatically transform into "Title Case".

UPPER CASE transformed to Title Case.
lower case transformed to Title Case.

Angelina Belle

Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

cosmicxxx

whew! what was that...

well, thank you. im not a coder... i did not understand that article.

Angelina Belle

Well -- you asked for a how-to.

It says that there is a helpful php function, called mb_convert_case, that can be used to do exactly what you want.
You could use it at the time the post is originally saved (in Post2), or else you could use it in a theme's template to display it that way without actually changing what is on the database.

I don't think there is a mod for this, or even a tip/trick.  Would you like me to move this discussion to the coding forum, where you might attract some more help?

Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

cosmicxxx

oh... yes please... do it so...

im learning basic sql, php, and html. self studying. right now i'm must trying to edit some mods, so that i can learn a bit and also learn some global functions of smf.


Angelina Belle

Enjoy learning.  I hope you come up with something good.
Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

IchBin™

ucwords() will do the same thing I think.

You could probably get away with adding that function to the $msgOptions array in Sources/Post.php where the subject is set.

   $msgOptions = array(
      'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
      'subject' => $_POST['subject'],

Where the subject would look like this:
      'subject' => ucwords($_POST['subject']),
IchBin™        TinyPortal

MrPhil

#7
No, ucwords() doesn't work the same as the requested "Title" case folding. ucwords() will uppercase the first letter of each word. In English, "title" case folding capitalizes the first word and then all words execept "the", "a", "an", "and", etc. (unimportant noise words). Other languages probably have similar rules.

Edit: ucwords(), not uwords()

cosmicxxx

so.. uwords or ucwords?

what i want to accomplish is what MrPhil is stressing out.

convert a subject title into a Title Case title...

for example:

YOU AND ME - shall be converted to => You and Me. and it should not be, You And Me.

and

you and me => to You and Me.

IchBin™

Do exactly as I showed you, only use the mb_convert function that Angel linked you to.
IchBin™        TinyPortal

cosmicxxx

alright... i'll give it a shot. but i think this is for expert coders.

Angelina Belle

Add only one line at a time, then test. You will catch your mistakes most easily that way.
You can handle this.
Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

IchBin™

You make a backup of your file before you make changes. If anything gets screwed up you can just upload the files you have a backup of to fix things.
IchBin™        TinyPortal

Angelina Belle

And you do all your testing on a COPY of your forum.
you only put the changes on your REAL forum when you are all done.
Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

cosmicxxx

thanks for all the tips...

i have a local dev webserver... i haven't tried it out yet, been busy for a while.

BeatsMe

Hi all, Sorry to pull up an old post.... But, i'd be interested in getting something like what was described here working.

I have tested what the instructions above said, and it works, here's the code that i used/changed...

// Collect all parameters for the creation or modification of a post.
$msgOptions = array(
'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
// 'subject' => $_POST['subject'],
'subject' => mb_convert_case($_POST['subject'], MB_CASE_TITLE, "UTF-8"),
'body' => $_POST['message'],
'icon' => preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),
'smileys_enabled' => !isset($_POST['ns']),
'attachments' => empty($attachIDs) ? array() : $attachIDs,
'approved' => $becomesApproved,
);


But, there are two problems with this code change...

1. It does not allow for acronyms such as IBM.  Not sure if this would be difficult to code?
and
2. I found that if you move the topic with rename option selected you can change the subject back to UPPER CASE and mb_convert_case doesn't re Title Case it.... obviously the move code is in another php file?

So, just wondering if any one has been working on this kind of idea, or could suggest what need to be changed to make this work better.

Cheers.

AMWebby

I tried the code above but it presents me with a blank screen after posting
Any idea why?

MrPhil

According to the PHP manual, MB_CASE_TITLE should uppercase every word, which does not follow the normal English rules for titles. It's not specified whether it first lowercases the words... if IBM is coming out as Ibm, it sounds like it does.

As PHP needs to be language-independent (English, French, etc.), I don't see how you can do this without writing your own routine, and depending on people to uppercase acronyms, etc. for you. That is, don't lowercase words first -- leave any existing capitalization as-is. Then follow the <language> rules for uppercasing just the first letters of most words. What you do with any "re" added to the beginning is your choice.

I'm wondering... for a post title, is it really appropriate to do Title Case? It often seems to be closer to a sentence than what some would call a title. I would be inclined to just leave it alone. If the member's use of cAPItalIZation seriously offends you, edit their post and leave a message why, to shame them.

@AMWebby's blank screen -- you probably introduced a coding error somewhere. Triple check your work. Hopefully you don't do your editing in Word or Outlook, as they will screw up the quotes.

AMWebby

#18
As I copied and pasted into Notepad++ I can't see any coding errors being introduced. I agree it looks like a coding error though.

Edit: Just tried creating a file called strings.php with the following generic code:


<?php
$str 
"mary had a Little lamb and she loved it so";
$str mb_convert_case($strMB_CASE_UPPER"UTF-8");
echo 
$str// Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
$str mb_convert_case($strMB_CASE_TITLE"UTF-8");
echo 
$str// Prints Mary Had A Little Lamb And She Loved It So
?>



Once again I got a blank page. Does this mean my version of PHP (4.1.14) doesn't contain mb_convert_case in the library?

Arantor

4.1.14? I hope you mean 5.1.14... but even that's several years old and unsupported (5.2 is unsupported, 5.3 is only receiving security fixes)

The thing is, mb_convert_case is a function of the mbstring library. Sounds like that's not installed (it's not installed by default on a lot of hosts)

On the other hand, you could always use ucwords(strtolower($str)) which for English would achieve virtually the same thing without having to reconfigure PHP...

MrPhil

mb_convert_case() was introduced with PHP 4.3.0, so if the OP is really way back on 4.1.14, that could explain it. Double check your PHP version, and whether mb_ functions are installed:
<?php phpinfo(); ?>

Arantor

There wasn't a version 4.1.14. If there was, it would have been 10 years ago, and there would be far greater issues with SMF than that.

AMWebby

#22
Thanks guys. Yes, the server is ancient and we're planning a move to a new server. Why didn't I think of php.inf, dih! In the meantime ucwords(strtolower($str)) works. Thanks.

Angelina Belle

In any case, none of these functions can truly achieve the desired title case.
"Mary Had A Little Lamb And She Loved It So" is the best it can do.
To get anything better, you would need, as Mr. Phil pointed out, a language-specific function that knows which are the "important" words that are supposed to be capitalized, and which are the unimportant.  Not only does the dictionary change from language to language, but also the rules for capitalization change (for example -- a lot more words are capitalized in German than in English).  And then -- what about acronyms that also spell words in the current language?  The rules grow more complex with every type of special case.

So this could be a fun learning-to-code exercise, but it will never get even 100% of the way to the desired result.
Unfortunately.

All the best to cosmicxxx, BeatsMe , and AMWebby.  At this time, there is no substitute for human intervention, if you want to enforce "proper" capitalization on your forum.
Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

Kindred

and what happens if I *WANT* an all caps word in the title?
or, if the proper spelling is all caps or all lower case?
or something like iPod?


You're trying to force your users into something which is likely to be incorrect as much as they are...
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Angelina Belle

Different forums.  Different users. Different rules.
In any case -- no matter how appropriate and/or important "title case" might be to a given forum, there is no technical solution available to bring it on.
My suggestion of mb-convert-case.php  was might be the WORST possible proposed semi-solution.
Arantor's is probably much better (because it is going to work on more php installations, even if it doesn't do 100% what the OP wants to do).



Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

AMWebby

I know I'm resurrecting an old thread but thought it best to stay on topic.

I'm trying to modify the code so that the title case function only works for one particular user. This is my code in Posts.php but it doesn't seem to work.


if ($context['user'] ['id'] == 6)
                {
                 'subject' => ucwords(strtolower($_POST['subject'])),
                }
                else
                {
                'subject' => $_POST['subject'],
                }


Anyone?

Arantor

Where *exactly* in Post.php is that?

AMWebby

Line 1869 on


// Collect all parameters for the creation or modification of a post.
$msgOptions = array(
'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
// string games
                if ($context['user'] ['id'] == 6)
                {
                 'subject' => ucwords(strtolower($_POST['subject'])),
                }
                else
                {
                'subject' => $_POST['subject'],
                }
                // end string games
'body' => $_POST['message'],
'icon' => preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),
'smileys_enabled' => !isset($_POST['ns']),
'attachments' => empty($attachIDs) ? array() : $attachIDs,
'approved' => $becomesApproved,
);

Arantor

Yeah, that's not going to work, you can't just smash an if statement in the middle of another statement. However you can do it this way:

// Collect all parameters for the creation or modification of a post.
$msgOptions = array(
'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
'subject' => $context['user']['id'] == 6 ? ucwords(strtolower($_POST['subject'])) : $_POST['subject'],
'body' => $_POST['message'],
'icon' => preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),
'smileys_enabled' => !isset($_POST['ns']),
'attachments' => empty($attachIDs) ? array() : $attachIDs,
'approved' => $becomesApproved,
);

AMWebby

Ah, my bad. Thanks for the recode.

Advertisement: