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.
{/code}

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.
{/code}

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.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *