Use PHP to Capitalise Your Text

If you want to make sure your text is nicely formatted there are a couple of tricks using PHP that can help you.

Changing the case of your words is one useful feature, that can be achieved with the functions in the following example.

{code type=php}
echo strtoupper(‘abc’); # outputs: ABC
echo strtolower(‘ABC’); # outputs: abc
echo ucfirst(‘frogs eat dogs.’); # outputs: Frogs eat dogs.
echo ucwords(‘frogs eat dogs.’); # outputs: Frogs Eat Dogs.

One thing to watch out for is if your text is uppercase (or mostly uppercase) and you want to use ucfirst() or ucwords() to capitalise the first letters – you won’t get the desired effect. Ensure you use strtolower() first, as follows:

{code type=php}
echo ucfirst (‘FROGS EAT DOGS ON LOGS.’); # outputs: FROGS EAT DOGS ON LOGS.
echo ucfirst (strtolower(‘FROGS EAT DOGS ON LOGS.’)); # outputs: Frogs eat dogs on logs.

Looking after the presentation of your text like this can make a huge difference to the perceived quality of your site. If you allow users to enter their own text at all, then it is a great idea to capitalise it using these php functions.


Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,


If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the