News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

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...

Advertisement: