PHP String Manipulation

When working with a string of characters, PHP has a number of built in functions that allow for them to be easily manipulated.

If it is necessary to output a basic string to a web page, the 'echo' and 'print' statements can be used.

echo "Hello world!";
print "Hello world!";

Both of these statements will output the message, 'Hello world!'. If it is necessary to incorporate variables into this message, then either 'echo' or 'print' can be used to do this.

$fname = "Bob";

echo "Hello " . $fname . "!";
print "Hello " . $fname . "!";

Here the '.' is used to concatenate the variable into the string. There is also a third method that facilitates the incorporation of a variable into a string, which utilises the 'printf' function.

printf("Hello %s!", $fname);

The '%s' acts as a placeholder, which is replaced by the variable specified after the comma and is known as a type specifier. In this case the 's' refers to the fact that the value being incorporated into the string is another string. The table below shows the different type specifiers that are available.

b Argument is formatted as a binary integer.
c Argument is formatted as a character with an ASCII value of the argument.
d Argument is formatted as a signed decimal integer.
e Argument is formatted in scientific notation.
f Argument is formatted as a floating-point number, using the Operating System regional settings.
F Argument is formatted as a floating-point number, but ignores the Operating System regional settings.
o Argument is formatted as an octal integer.
s Argument is formatted as a string.
u Argument is formatted as an unsigned decimal integer.
x Argument is formatted as a lower case hexadecimal integer.
X Argument is formatted as an upper case hexadecimal integer.

If it is necessary to incorporate more than one variable into the string, the variable names to incorporate would be separated by commas.

$fname = "Bob";
$age = 30;

printf("%s is %d years old.", $fname, $age);

Note that, there is also a variation on the 'printf' function, 'sprintf', which allows the resulting formatted string to be assigned to a variable.

$formattedString = sprintf("%s is %d years old.", $fname, $age);

Index Value

Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on.

$example = "This is a string.";

echo $example[3];

Here, the index value of three in square brackets is used to output the fourth character of the string.

It is also possible to find the index position of a character in a string using the 'strpos' function.

echo strpos($example, 'i');

There are three things to note with the 'strpos' function. Firstly, if there are two occurrences of the character, in this case 'i', then the index of the first occurrence will be returned. Secondly, 'strpos' is case-sensitive, so if 'i' was replaced with 'I' in the above example, it would not be found. A null value is returned if the character does not exist in the string. Finally, if the function is being used to find the position of a string of characters rather than a single character, then the index position returned is where the string starts.

If a case-insensitive search for a character, or string within a string is required, then the function 'stripos' can be used in the same manner as 'strpos'.

echo stripos($example, 't');

If it is necessary to find the last occurrence of a character, or substring, within a string, the functions 'strrpos' or 'strripos' can be used, with 'strrpos' being case-sensitive and 'strripos' being case-insensitive.

echo strrpos($example, 't');
echo strripos($example, 't');


Using the 'substr' function, it is possible to return a portion of a string by specifying the index position to start from, together with the number of characters that are required.

$example = "This is a string.";

echo substr($example, 5, 4);

The above example starts at index position five and displays four characters. Note that, if the second value for the number of characters to display is omitted, then the rest of the string will be displayed.

is a

Upper and Lower Case

In order to change a string to upper or lower case, there are two functions that can be used, 'strtoupper' and 'strtolower'.

$example = "This is a string.";

echo strtoupper($example);
echo strtolower($example);

The first will change the whole string to upper case and the latter will convert it to lower case.

this is a string.


If a string has extra spacing at the start, end, or both, it's possible to remove this using the 'trim' function as demonstrated below.

$example = "   This is a string.   ";

echo trim($example);

If only the extra spacing at the start or end needs to be removed, then the 'ltrim' and 'rtrim' functions can be used to remove it from the start and end respectively.

echo ltrim($example);
echo rtrim($example);


Sometimes it is essential to find the size or length of a string. This can be done using the 'strlen' function.

$example = "This is a string.";

echo strlen($example);


The 'str_replace' function can be used to replace a specified part of a string with some other text.

$example = "This is a short string.";

echo str_replace("short", "small", $example);

Here, the word 'short' is replaced with the word 'small' to produce the string, 'This is a small string.'. If 'short' appeared more than once in the string, it would replace all occurrences. As with the 'strpos' function, discussed above, 'str_replace' is case-sensitive, however, PHP also provides a case-insensitive version, 'str_ireplace'.

Further Reading