TOC

The community is working on translating this tutorial into Persian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Data types:

More strings

PHP has a huge set of functions for working with strings. It's beyond the scope of this tutorial to explain them all, but in this chapter, we will have a look at some of the more useful string related functions.

The length of a string

In some cases, it can be useful to know the exact length of a string, that is, how many characters it contains, including spaces. You may use the strlen() function for this purpose, as illustrated in this example. The function takes only one parameter, and that is the string variable to measure.

<?php
$string = "Hello world!";
echo "Length of string: " . strlen($string);
?>

Lowercase and uppercase

Sometimes you may need to convert your string to either an uppercase or a lowercase string. Fortunately, it's very easy:

<?php
$string = "Hello world!";
echo strtolower($string);
echo "<br />";
echo strtoupper($string);
?>

The first function returns an all lowercase version of the string, while the second one returns an uppercase version. We echo out the result immediately.

Find a part of a string

If you need to find the position of a part of a string, within a larger string, you may use the strpos() function like this:

<?php
$string = "Hello world!";
echo "Position of world in string: " . strpos($string, "world");
?>

Be aware that it's case sensitive, or in other words, "world" and "WORLD" are not the same. If you wish to look for a part of a string and case shouldn't matter, use the case-insensitive version of the function instead. It's called stripos(). We use only two parameters, but a third optional one is available, allowing you to tell PHP where to start the search. It's not relevant to this example though.

Retrieve a part of a string

In the last example, we found a part of the string. However, you may need to get an actual part of the string and use it for something else. The substr() function can help you with that, and in the next example, we use it along with two of the functions we have already covered:

<?php
$string = "Hello world!";
$startPos = strpos($string, "world");
$length = strlen("world");
$substring = substr($string, $startPos, $length);
echo "A specific part of the string: " . $substring;
?>

Okay, the example might not seem terribly useful right now, and we do make it a bit harder for our self, e.g. by measuring the length of a string that we could count in a second and just use the actual number. However, it does show you how you can use string functions in combination to achieve something that could be rather useful in a more complicated situation.

There are many more string related functions, but some of them are very specific and only useful in few cases. You can have a look at the PHP manual if you would like to know more.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!