0

I am a newbie and this is a tough one for me.

I have a text inside a variable:

$bio = 'text, text, tex ...';

I can use the ucfirst php function to make word in the text start with an uppercase letter.

The problem is I don't want the words with one, two or three letters be capitalized becouse it would look unprofessional.

IMPORTANT: But I want also to keep the letter "I" capitalized since it's proper english grammar.

So a text like: this is a text without ucfirst function and i think it needs some capitalizing

Would look like: This is a Text Without Ucfirst Function and I Think it Needs Some Capitalizing

Any ideas?

webmasters
  • 5,663
  • 14
  • 51
  • 78
  • 2
    If you are aware of `if conditions` you know enough to attempt something. Show us what you have attempted. – Ayush Jun 07 '12 at 00:17
  • funny definition of professional here. your capitalisation does not confirm to any English grammar I have been taught. –  Jun 07 '12 at 00:18
  • :) true Dagon, its for SEO titles which look better with their keywords upper case – webmasters Jun 07 '12 at 00:20
  • from the "would like" example it seems you need ucwords function, not ucfirst (as a start) –  Jun 07 '12 at 00:20
  • looks better to? humans (not this one) bots- don't care, SEO- mostly rubbish anyway –  Jun 07 '12 at 00:26

3 Answers3

3

This will capitalize any word (sequence of English letters) that is 4 or more letters long:

$bio = preg_replace_callback('/[a-z]{4,}|\bi\b/i', function($match){
    return ucfirst($match[0]);
}, $bio);

For PHP versions before 5.3:

$bio = preg_replace_callback('/[a-z]{4,}|\bi\b/i',
  create_function('$match', 'return ucfirst($match[0]);'), $bio);

It will leave any shorter words as is such as I and add, and capitalize i.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Ty very much, will this keep the sigle letter word "i" uppercase? – webmasters Jun 07 '12 at 00:21
  • @webmasters It will leave it as is. If it is input as `I` it will stay as `I`. If it is input as `i` it will stay like that. I believe that is what you wanted since you said "*keep* it capitalized" in your question. If you want it to make `i` capital then let me know. – Paul Jun 07 '12 at 00:22
  • I capital and ty very much for helping – webmasters Jun 07 '12 at 00:23
  • @webmasters You are welcome, but I don't know what you mean by "I capital". Are you saying the input `I` is already capitalized, or are you saying you need me to change my answer to make it capitalize `i`? – Paul Jun 07 '12 at 00:24
  • the I should always be capitalized, no matter of the input – webmasters Jun 07 '12 at 00:25
  • so if the text it "i am legend" it would become "I am Legend" – webmasters Jun 07 '12 at 00:25
  • @webmasters Ah I see. I added a line to my answer so now it should do that :) – Paul Jun 07 '12 at 00:26
  • `/([a-z]{4,}|\bi\b)/i` --- or you could fit it in regexp, without second `preg_replace` – zerkms Jun 07 '12 at 00:27
  • Ty very much, you are a life saver. I am just a webmaster and mostly SEO and I'm not very good as php stuff but try to return the favor for others interested in my field. – webmasters Jun 07 '12 at 00:27
  • And the first part of regex could be better in the form `\b([^\b]{4,})\b` – zerkms Jun 07 '12 at 00:28
  • @zerkms Yes, I did that now. That is better. I guess I singled it out as a separate problem after the conversation in comments and did it on its own :) – Paul Jun 07 '12 at 00:28
  • Tried to test it and dreamweaver gives me an error. Any ideas why? I just put this inside my code under the $bio = 'text,tex...' variable. – webmasters Jun 07 '12 at 00:30
  • I'm not familiar with dreamweaver. Does it run your PHP? – Paul Jun 07 '12 at 00:33
  • It's just an editor, like wordpad, which tells me when there is an error in the code. – webmasters Jun 07 '12 at 00:34
  • @webmasters Is it configured for PHP 5.3? Anonymous functions didn't exist before PHP 5.3, so it would be a syntax error then. – Paul Jun 07 '12 at 00:35
  • @webmasters Do you know what version of PHP is on your server? Or can you check? You should try to find an option in Dreamweaver to specify PHP version and make sure it matches. Also if your server is below PHP 5.3 let me know so I can edit my answer again haha – Paul Jun 07 '12 at 00:41
  • It is, i am hosting my site at cyberwurx. :( and ty very much for being patient – webmasters Jun 07 '12 at 00:44
  • @webmasters I see. I updated my answer to work with older versions of PHP, however I also recommend that you upgrade your PHP version or contact your Web host and tell them it's time to upgrade. PHP 5.3 was first released almost 3 years ago and it is important to keep open source software like this up to date, as many security vulnerabilities have been found and fixed since then. – Paul Jun 07 '12 at 00:49
0

I would use regex. If you don't want to, you could use split, then iterate over the tokens and use a bunch of if-else, but regex is cooler. ;)

There is a very similar question here: Regex capitalize first letter every word, also after a special character like a dash

Community
  • 1
  • 1
Turnsole
  • 3,422
  • 5
  • 30
  • 52
0

Mmmm so?

$bio_x = explode(" ",$bio); if(strlen($bio_x[0]) > 3) $bio = ucfirst($bio); $bio = str_replace(" i "," I ",$bio);
Sam
  • 2,950
  • 1
  • 18
  • 26