September 24, 2016 feature-image

Handy String Manipulation Functions in PHP

Handy String Manipulation Functions in PHP 5 that every PHP programmer should know.

First off, let’s define what a string is:

In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation). More info on this here.

Now that we know what a string is by definition, let’s take a look at the different functions that can come in handy at one point or another while we program in PHP.
Let’s explain these functions below.

If at any point, you would like to get the length of a “string” or a “word”, you can use the following function:


By using this function, you are able to get the length of any string.
A quick example would be:

The above code will display on the browser: 12.


Ever wondered how to reverse a string in PHP? Well here is the most easiest way to do so. Look at the following example:

The output from above will display the following:

beW serrotoJ.


Another function that could come in handy at some point is the word count. As you can guess, this will count the number of words within a string. Let’s take a look at how this can be used.


The string in the above example was counted and yes, as you guessed, it will out put the number 2. Numbers within the string, will not be considered as a word, so be leery when utilizing this function.


Sometimes, we need to find just a ‘portion’ of a string. This is called a substring. The function just mentioned returns the character position of the first occurrence of a substring whitin a string if a match is found. Otherwise, this function will return a FALSE value.

The example code from above will search within the given string the word “Web”. The output of this will be 9, taking into account that strings are nothing more than just an array of characters, and the first character is in position 0(zero), not 1(one).


To replace characters within a string in PHP, you can use the following function.

The previous example will output “Jotorres PHP”. This function is case sensitive. If at any time you would need to use this same function without having to worry about casing, you could use the function of STR_IREPLACE().

Other functions that could be used in replacing string are the following:

SUBSTR_REPLACE() : To replace a text within a portion of a string.
PREG_REPLACE(): Performs a regular expression search and replaces it.

To learn more about string functions in PHP, you can always look them up here.

Tagged on:

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.