PHP String Functions

Chapter 19 33 mins

Learning outcomes:

  1. Changing the casing using strtolower() and strtoupper()
  2. Trimming characters using trim(), ltrim() and rtrim()
  3. Searching for substrings using str_contains(), strpos(), stripos(), str_starts_with() and str_ends_with()
  4. Slicing a string using substr()
  5. Repeating a string using str_repeat()
  6. Counting substrings using substr_count()
  7. Splitting strings using str_split() and explode()
  8. Replacing substrings using str_replace()

Introduction

PHP hosts a huge collection of string functions to allow us to fluidly work with strings in the language. Some functions are so complex that we need whole chapter just to explain their underlying purpose. Fortunately, we won't be covering this level of complexity in here.

In this chapter, we'll explore a handful of string functions in PHP, ranging from functions to modify the casing of strings, to functions meant to search for given substrings within strings, to functions to slice strings from given starting positions to given ending positions, and so on.

We'll also see how to count all occurrences of a substring inside a string, in addition to how to repeat a string n number of times. Replacing certain substrings from larger strings is also a common activity which we'll see how to do in this very chapter.

There are just a whole lot of functions to explore, so let's get started.

Lower and upper casing

Converting the casing of strings is an extremely useful activity in nearly all programming languages that deal with strings, even SQL.

To convert a string to all lowercase characters in PHP, we use the strtolower() function. It takes in the string as an argument and then returns back a new string, in all lowercase characters.

Consider the snippet below:

<?php

$str = 'JavaScript';
echo strtolower($str);
javascript

As stated before, the strtolower() function returns a new string — the original string is kept in tact. The example below illustrates this:

<?php

$str = 'JavaScript';

echo strtolower($str), "\n";
echo $str;
javascript JavaScript

The value of $str is the exact same as it was before calling strtolower(), which confirms that it isn't modified by the function.

Similar to strtolower(), strtoupper() is used to convert a string to all uppercase characters.

Here's an example of strtoupper():

<?php

$str = 'JavaScript';
echo strtoupper($str);
JAVASCRIPT

The strtolower() function is commonly used to normalize the casing of arbitrary strings when comparing them.

Shown below is a fully-fledged example:

<?php

$cities = ['London', 'Paris', 'New York', 'Tokyo'];

echo 'Enter a city: ';
$input_city = rtrim(fgets(STDIN));

$city_exists = false;
for ($i = 0; $i < count($cities); $i++) {
   if ($cities[$i] === $input_city) {
      $city_exists = true;
   }
}

if ($city_exists) {
   echo $input_city . ' exits.';
}
else {
   echo $input_city . ' does not exist!';
}

When the user enters the name of a city, it's determined whether the input exists in the list $cities.

Let's say that the input value is 'london'; it won't yield a match because strictly-speaking there's nothing such as 'london' in $cities.

Enter a city: london london does not exist!

However, ideally this shouldn't be the case — even 'london' should be considered as a match for 'London', ignoring the casing of the word.

The way to solve this problem is to compare the strings while they are both lowercased. The example below illustrates this:

<?php

$cities = ['London', 'Paris', 'New York', 'Tokyo'];

echo 'Enter a city: ';
$input_city = rtrim(fgets(STDIN));

$city_exists = false;
for ($i = 0; $i < count($cities); $i++) {
if (strtolower($cities[$i]) === strtolower($input_city)) { $city_exists = true; } } if ($city_exists) { echo $input_city . ' exits.'; } else { echo $input_city . ' does not exist!'; }
Enter a city: london london exists.

This time, the output clearly indicates that the city 'london' was found in the $cities array.

Moving on, if we only want to convert a string's first character to lowercase or uppercase, we can use the lcfirst() or ucfirst() functions, respectively.

The snippet below demonstrates this:

<?php

$str = 'hello';
$str2 = 'World';

echo ucfirst($str), ' ', lcfirst($str2);
Hello world

Trimming characters

Often times as we obtain input from the user, it's desirable to trim off any extra whitespace from either end of the string before we process it any further.

To do so in PHP, we have the provision of the trim() string function.

Here's the signature of trim():

trim($string, $characters = " \n\r\t\v\x00")

The first argument is the string to trim while the second argument is a string defining all the individual characters to trim.

If the second argument is omitted, it defaults to a string containing all different kinds of whitespace characters. Thus trim() without the second argument effectively trims whitespace off from both ends of the given string $string.

The example below illustrates trimming whitespace off from a string:

<?php

$str = '    Trimming     ';

echo $str . ' whitespace.', "\n";
echo trim($str) . ' whitespace';
Trimming whitespace. Trimming whitespace.

In the first echo statement, we concatenate $str with ' whitespace' without trimming $str, and thus get the oddly-formatted first line of output. But then, we trim $str and repeat the same process — this time the output looks nice, with all the extra whitespace trimmed from both of the ends of $str.

Let's now try providing the second $characters argument to trim().

We have got to know that a string $str contains some unnecessary hyphens (-) on both of its ends that we must trim off. Fortunately, using trim(), this is very easy to do:

<?php

$str = '----PHP----------------';
echo trim($str, '-');
PHP

One thing worth mentioning here is that the $characters argument to trim() can even define a range of characters to trim using ...

So, for example, if we want to trim all digits from the ends of a string, we don't have to manually type all the digits from 0 to 9, as shown below:

<?php

$str = '08679Hello World!41238945';
echo trim($str, '0123456789')
Hello World!

Instead, we could use .. to define all the digits from 0 to 9 using 0..9, as follows:

<?php

$str = '08679Hello World!41238945';
echo trim($str, '0..9')
Hello World!

Moving on, if trimming is desired on a particular end of a string, PHP also provides us with two separate functions to perform it. They are ltrim() and rtrim() (which you have already worked with).

As is evident by the names, ltrim() works on the left side of the string whereas rtrim() works on its right side.

Here's a quick example of ltrim() and rtrim():

<?php

$str = '----PHP----------------';

echo ltrim($str, '-'), "\n";
echo rtrim($str, '-');
PHP---------------- ----PHP

Searching for substrings

Searching for given substrings inside strings is a routine activity done in all kinds of programs.

Sometimes, we want to determine the position of the substrings inside the main string, while sometimes we are only concerned with whether or not the substrings exist, without wanting to know their exact position.

PHP provides a handful of functions to search for substrings in strings.

They are given as follows:

  • str_contains() — determines whether or not the substring is in the string.
  • strpos() — finds the index of the substring inside the main string.
  • stripos() — same as strpos() except for that it works case-insensitively.
  • str_starts_with() — checks whether a string starts with a given substring.
  • str_ends_with() — checks whether a string ends with a given substring.

Let's explore each one of these starting with str_contains().

str_contains()

We've already seen str_contains() in action in the previous chapter, so let's quickly review it.

The function takes two argument as shown below:

str_contains($string, $substring)

The first argument is the string in which we want to conduct a search while the second argument is the substring to search. The most important thing to note about str_contains() is that it merely returns a Boolean depending on whether the string contains the given substring or not.

Shown below is an example:

<?php

$str = 'JavaScript';

var_dump(str_contains($str, 'Java'));
var_dump(str_contains($str, 'JavaScript'));
var_dump(str_contains($str, 'script'));
bool(true) bool(true) bool(false)

Remember that str_contains() operates case-sensitively, which is apparent by the third statement.

strpos() and stripos()

The strpos() function stands for 'string position', and serves to return the position of a substring inside the main string if it includes the substring.

If you're familiar with JavaScript, strpos() is equivalent to the string method indexOf() from JavaScript.

Here's the syntax of strpos():

strpos($string, $substring, $index = 0)

The first argument is the string in which we want to search for a substring while the second argument is that substring. The optional third $index argument specifies the position where the search must begin. It defaults to 0.

strpos() returns an integer representing the index where the substring appears inside the main string, or else the value false.

Let's take a couple of examples to help illustrate strpos():

<?php

$str = 'PHP is amazing and is great.';

var_dump(strpos($str, 'is'));
var_dump(strpos($str, 'amazing'));
var_dump(strpos($str, 'eat'));
var_dump(strpos($str, 'php'));
int(4) int(7) int(24) bool(false)

If we want to perform searching case-insensitively, we could consider using the stripos() function. It works exactly like strpos(), just that it ignores the casing in the search.

In the following code, we try searching for 'php' inside the string $str using stripos():

<?php

$str = 'PHP is amazing and is great.';

var_dump(strpos($str, 'php'));
var_dump(stripos($str, 'php'));
bool(false) int(0)

str_starts_with() and str_ends_with()

If we wish to just determine whether a given string starts with a particular substring, we can easily do so just using strpos() and the identity operator (===) in PHP.

For example, in the following code, we check if the string $lang begins with 'Java', using strpos() and a mere comparison:

<?php

$lang = 'JavaScript';

var_dump(strpos($lang, 'Java') === 0);
bool(true)

Fortunately, PHP provides a function to perform exactly this kind of a check — str_starts_with().

str_starts_with($string, $substring)

As the name suggests, str_starts_with() checks if a string starts with a particular substring or not. It returns back a Boolean to signify the result.

The same code above could be rewritten as follows using str_starts_with():

<?php

$lang = 'JavaScript';

var_dump(str_starts_with($lang, 'Java'));
bool(true)

Similar to str_starts_with(), PHP also has a function to check if a string ends with a given substring. It's called...you guessed it...str_ends_with().

Syntactically, str_ends_with() is identical to str_starts_with().

The code below demonstrates an example of using str_ends_with():

<?php

$lang = 'JavaScript';

var_dump(str_ends_with($lang, 'Script'));
bool(true)

Slicing a string

Slicing a string refers to extracting at a substring from a string, starting and ending at given positions. For instance, given the string 'JavaScript', if we were to slice this from the index 0 (inclusive) to the index 4 (exclusive), we'd get 'Java'.

Slicing strings is another common operation performed when working with strings in any programming language. In PHP, we can slice a string with the help of the substr() function.

As the name suggests, substr() serves to extract out a substring from a given string, hence the name 'substr'.

Here's the syntax of the function:

substr($string, $index, $length)

The first $string argument is the string to slice. The second argument specifies the index where the slice ought to begin. The third $length argument specifies the length of the slice.

In the code below, we make a bunch of slices of the $lang string:

<?php

$lang = 'Hello World!';

echo substr($lang, 1), "\n";
echo substr($lang, 6), "\n";
echo substr($lang, 6, 2), "\n";
echo substr($lang, 6, 1);
ello World! World! Wo W

If we want to, the third $length argument to substr() can also be negative. In that case, it is taken to denote the length of the string to trim away from the end of the string.

Consider the following code:

<?php

$lang = 'Hello World!';

echo substr($lang, 1, -1), "\n";
echo substr($lang, 0, -3), "\n";
echo substr($lang, 6, -1)
ello World Hello Wor World

The first substr() call effectively slices the string $lang from index 1 to just before the last character of the string. Similarly, the second call throws away the last three characters from the slice. In the third call, the slice begins from index 6 and ends just before the last character of the string.

If the $length argument is such that the ending point of the slice becomes less than than the slice's starting point, an empty string gets returned.

This is demonstrated below:

<?php

$lang = 'Hello World!';

var_dump(substr($lang, 7, -4));
var_dump(substr($lang, 7, -5));
string(1) "o" string(0) ""

The first argument yields merely a single character starting at index 7, i.e. 'o'. And then the next call, with $length representing the ending index as 7 as well, we get an empty string returned.

Repeating a string

The str_repeat() function can be used to repeat a string a given number of times.

Repeating strings might not be a very common concern while dealing with strings, but if we ever want to do it, at least we know that we have a function for it.

str_repeat($string, $count)

The first argument, as always, is the main string to repeat, while the second $count argument is an integer specifying the repeat count. If the repeat count is 0, an empty string is returned.

Consider the following code where we repeat the string $str 10 times:

<?php

$str = 'OK';

echo str_repeat($str, 10);
OKOKOKOKOKOKOKOKOKOK

Since the length of $str is 2, the length of the string returned by str_repeat() is 20 — basic math.

Counting substrings

Let's say we have a string and want to count how many times does a given substring occur within it. Well, one way is to use a while loop and search for all occurrences of the substring using strpos(), as we discovered above.

But we won't do that because PHP provides a much simpler function to do so — say hello to substr_count().

As the name implies, substr_count() serves to count the number of substrings inside a given string.

Keep in mind that the function searches for the substrings case-sensitively.

Here's its signature:

substr_count($string, $substring, $offset = 0[, $length])

The first two arguments specify the main string and the substring to count for in the main string, respectively.

The last two arguments act, more or less, as if they first slice the string $string and then perform a search over it for $substring. In that way, both of these arguments work like the same arguments of substr().

Let's consider an elementary example using substr_count():

<?php

$str = 'This is it and it is to admit.';

echo substr_count($str, 'it');
3

The substring 'it' appears 3 times inside the string $str and thus substr_count() yield 3.

Now, let's bring on the third and fourth arguments into the equation:

<?php

$str = 'This is it and it is to admit.';

echo substr_count($str, 'it', 10), "\n";
echo substr_count($str, 'it', 10, -3);
2 1

The first substr_count() call here begins searching after the first occurrence of the substring 'it' in $string, which is from index 10, and hence we get 2 matches of the substring.

The second call is the same as the previous one, except for that it ends before the third occurrence of the substring; hence we get only 1 match returned by this call.

Not really difficult to understand, was it?

Splitting strings

Converting a string into an array of characters or into an array of substrings, broken down at given delimiters in the string, is an important operation.

It's often required when we wish to process each of the characters or substrings separately, and then possibly obtain an array back out of them by joining them together into a string.

This action of converting a string into an array of substrings is sometimes referred to as splitting the string.

PHP comes equipped with a bunch of functions to split a string apart into an array of substrings:

  • str_split() — splits a string into given lengths of substrings.
  • explode() — splits a string at given delimiters.

Let's explore each of these.

str_split()

The str_split() function is used to split apart a string at every other character, by default. If we want to, we can even specify the maximum length of each substring split.

Here's the syntax of the function:

str_split($string, $max_length = 1)

$string is the string to split whereas $max_length is the maximum length of each substring. The default value of $max_length is 1, which means that the string gets split into individual characters.

The function str_split() returns back an array containing all the substrings.

In the following code, we split the string $code into an array of characters and then convert each character into a number, before processing the number by adding 1 to it:

<?php

$code = '16489';
$code_arr = str_split($code);

for ($i = 0; $i < count($code_arr); $i++) {
   // Conver the character to an integer, and then add 1 to it.
   $code_arr = ((int) $code_arr[$i]) + 1;

   echo $code_arr[$i];
}
275910

As each item in the array $code_arr is processed in the for loop, it's echoed right at the same time so that we are able to join all of the resulting values in the output.

As we shall explore later on in the unit on arrays, the implode() function can be used to join together an array of substrings into a string, with a delimiter in between. In the code above, instead of echoing the resulting items in order to join them, we could call implode() on $code_arr to join them together into a string, and then possibly work with that string.

Let's now experiment with the $length argument of str_split().

<?php

$str = 'Hello PHP!';

print_r(str_split($str, 3));
Array ( [0] => Hel [1] => lo [2] => PHP [3] => ! )

As can be seen here, the last item of the returned array has a length of 1, and that's because there were simply no characters left in the string.

This is the reason why we call the second arg as $max_length, instead of $length — because it literally represents the maximum length of each substring in the returned array.

explode()

Where the str_split() function is used to split a string into given length of substrings, the explode() function is used to split it at given characters, otherwise known as delimiters.

For instance, if we have a string containing a list of words, each separated from another word by a comma (,), we could use explode() in order to extract all the words from the string.

Note that explode() can't be used to convert a string into an array of characters. Likewise, str_split() has its significance.

Here's the signature of explode():

explode($delimiter, $string, $limit = PHP_INT_MAX)

Contrary to the norm, the first argument here is not the main string to process; instead it's a string specifying the delimiter at which the main string must be split apart. The second argument is the main string to explode into individual substrings.

The optional third $limit argument specifies the limit of exploded substrings. By default, this limit is really huge; specifically, it's PHP_INT_MAX.

Let's consider an example of using explode() to break apart a string into an array of substrings:

<?php

$fruits = 'oranges,bananas,apples,kiwis,melons';

print_r(explode(',', $fruits));
Array ( [0] => oranges [1] => bananas [2] => apples [3] => kiwis [4] => melons )

As you can see here, explode() has allowed us to neatly extract out all of the words from the $fruits string into an array.

Moving on, if explode() is called with the $delimiter argument as an empty string (''), an error is thrown.

This is demonstrated in the following code:

<?php

$fruits = 'oranges,bananas,apples,kiwis,melons';

print_r(explode('', $fruits));
PHP Fatal error: Uncaught ValueError: explode(): Argument #1 ($separator) cannot be empty in <path> ...

Replacing substrings in strings

The str_replace() function is used to replace occurrences of substrings from a given string. The function returns back a new string containing the replacements; the original string remains in tact.

Let's see the signature of str_replace():

str_replace($search, $replace, $string, &$count)

Akin to explode(), the first argument to str_replace() is not the main string; it's instead the substring to search for in the main string. The second argument is the replacement substring. The third argument is the main string where the replacements ought to be performed.

The fourth reference argument $count receives the number of replacements performed in the main string.

Precisely speaking, the $search and $replace arguments to str_replace() can be arrays as well, but these represent a slightly advanced notion, and so we'll consider them later on.

For now, let's consider the simpler use case.

In the following code, we replace every occurrence of 'JavaScript' in $str with 'Java':

<?php

$str = 'JavaScript was first released in 1995. JavaScript is amazing.';

echo str_replace('JavaScript', 'Java', $str);
Java was first released in 1995. Java is amazing.

Since the number of replacements performed are 2, if we pass a fourth argument, it'll be assigned the value 3 by the str_replace() function, as demonstrated below:

<?php

$str = 'JavaScript was first released in 1995. JavaScript is amazing.';

$count = 0;
echo str_replace('JavaScript', 'Java', $str, $count), "\n";
echo $count;
Java was first released in 1995. Java is amazing. 2

As can be seen here, the value of $count becomes set to 2, i.e. the number of replacements performed in the str_replace() call, after the function completes.

Now, let's take a look at an example where $search and $replace are both arrays.

In the following code, we aim to replace the substrings 'JavaScript' to 'Python', while the year '1995' to '1991' (as Python was first released in 1991):

<?php

$str = 'JavaScript was first released in 1995. JavaScript is amazing.';

echo str_replace(['JavaScript', '1995'], ['Python', '1991'], $str);
Python was first released in 1991. Python is amazing.

Each item in $search is matched with the corresponding item in $replace. Likewise, the first item of $search, i.e. 'JavaScript', gets replaced with the first item of $replace, i.e. 'Python', and so on.

Even multiple search values could be replaced with the same substring, in which case $replace is just a string while $search is an array of all the values to search for.

Shown below is an example where we replace the names 'Java', 'Python' and 'C' with the word 'PHP':

<?php

$str = 'Java is good. Python is also good. C is great.';

echo str_replace(['Java', 'Python', 'C'], 'PHP', $str);
PHP is good. PHP is also good. PHP is great.