LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2010, 02:04 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
How to capitalise first letter in PHP


Hi,
I have a string such as "helLo wOrlD". I'd like to transform this into "Hello World", ie. capitalise the first letter of every word, and transform the rest into small letters, both being compulsory.
How do I do it?


Thanks.
 
Old 07-28-2010, 02:10 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
These should help:

http://www.php.net/manual/en/function.strtolower.php
http://www.php.net/manual/en/function.ucfirst.php

Always try looking at the documentation first!
 
Old 07-28-2010, 03:22 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Googling for "php+proper()", I came across this as the first suggestion. It might also be what you need.

Last edited by AnanthaP; 07-28-2010 at 03:23 AM. Reason: Incomplete
 
Old 07-28-2010, 06:28 AM   #4
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
Quote:
Originally Posted by AnanthaP View Post
Googling for "php+proper()", I came across this as the first suggestion. It might also be what you need.
as also the link in the text says, ucwords() does exactly that, so his workaround you posted is quite unnecessary

ucword() for the first letter in a string, ucwords for every word's first letter in a string
 
Old 07-28-2010, 07:23 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You know I find it quite funny that search engines only work on certain continents, in certain states and even sometimes as close as a few suburbs away (as has been my experience). Personally I have yet to come across one of these magical places that allows me to frequent all site on the internet except any of the search engines, even to the point where I go to a site with a search engine built in (say www.linuxquestions.org) and for the life of me I can't access it either.

Well ... live and learn I guess.
 
Old 07-28-2010, 10:07 AM   #6
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
I was actually looking for a solution with regular expressions. PHP does have regex's, right?
 
Old 07-28-2010, 11:12 AM   #7
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
it has, look at preg_match(), preg_replace(), preg_match_all(), but out of curiosity, why use regex for this?
 
Old 07-29-2010, 09:07 AM   #8
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
Oh no particular reason, I've just done this sort of thing before in Perl, and there I used regex's, so I was looking to go the same way here.... , but the solutions suggested above will do just fine!
 
Old 07-29-2010, 09:49 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by resetreset View Post
Oh no particular reason, I've just done this sort of thing before in Perl, and there I used regex's, so I was looking to go the same way here.... , but the solutions suggested above will do just fine!
PHP isn't Perl, so things are done a bit differently.
 
Old 07-30-2010, 09:10 AM   #10
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
Quote:
Originally Posted by MTK358 View Post
PHP isn't Perl, so things are done a bit differently.
yeah it isn't, nonetheless it includes above mentioned functions (and couple more) to run regex stuff.
preg stands for, iirc, "perl regular" [expressions]

http://uk.php.net/manual/en/book.pcre.php
PCRE = Perl Compatible Regular Expressions
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Capitalise Only First Letter Of Each Word In A Sentence onesikgypo Programming 2 02-15-2009 11:41 AM
How to capitalise ONLY the first letter in Perl resetreset Programming 3 01-10-2009 11:19 AM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 07:18 PM
PHP delete letter from string Boffy Programming 2 04-30-2004 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration