November 29, 2011 learn_php

String manipulations in PHP

String manipulations in PHP

In many cases within our website, we might need to format some strings that the user has entered. I have put together just a few of these native PHP functions that will help out to our advantage and make our work look a lot nicer.

The first thing we should all know is, how to eliminate any excess whitespaces that might cause our process to be defective. With whitespaces within our strings, in some cases, might cause for your program or process to run incorrectly, due to some comparison of some sort. There are 3 native functions to achieve this.


The first of these is the basic trim() function. This function accepts a $string to format. This function will remove any whitespace(‘ ‘) that is not in between the string, tab(\t), new line(\n), carrige return(\r), a NUL byte(\0), and vertical tabs(\x0B). This function also takes an optional second parameter. If this second parameter is present, it will override the default character lists. Let’s pretend we have a string with hello world.

The output for this would be the simple hello world. It has eliminated all the default characters.

Hello World!

If we were to specify to only get rid of ‘\t’ then we would do it something like this:

This output is:


ltrim() and rtrim()

In case you needed to do this only to process the ‘left’ or the ‘right’ of a string, you can use these ltrim and rtrim. ltrim() eliminates characters from the left, as rtrim eliminates from the right of the string.


In case you are wondering, the acronym for this functions spells it out for you. nl2br just means ‘New Line to Break’, which converts the following:

All instances of new line '\n' to an XHTML line break '

In an html output, we would have the following:

The output for this would be:

Hello World!

print(), printf(), and sprintf()

We have seen in some examples within this website, that to display a string to a user in the browser, we use ‘echo’. PHP supports another way of doing this with print(). This print does exactly the same as the echo we have seen. In some cases, we would need to use a formatted string, in which case we can use printf() and sprintf(). Both of these work the same way, except that, printf will display a formatted string to the browser, and sprintf will return a formatted string. Using sprintf() can be very helpful when it comes to dynamic messages to send as an email. Let’s compose a simple message to send to a user:

This previous example gives us the full message with the name, date, and the subject. This message is now ready to be used for whatever reason you need.

Other useful functions

Other native functions for PHP are the following:
Putting the string to uppercase:



If you need the output to be all lowercase, then:

Resulting Ouput for this:

hello world

Capitalize only the first letter of the string:

Output would be:

Hello world

Capitalize the first letter of every word.

Output would be:

Hello World

Sometimes your process needs to know the exact amount of characters your string contains. This is achieved with the function strlen(). This accepts a string, and returns a numeric value of the total characters the string has, including whitepaces.

The output is:

In some cases, strlen is needed to compare and see whether a string is of a certain amount of characters.

And finally for today, if you needed the first 3 letters of a string, you can get them by using substr(). This accepts a required parameter, the string to be used, a number in where to start (0 zero for the first letter) and an ending number(in this case 3, since we want only the first 3 letters).



There are a lot more of these native constructs / functions in PHP that could be used for specific needs. In time, we will see more of these.

Hope you enjoyed.

2 thoughts on “String manipulations in PHP

Leave a Reply

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

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.