Current Location: Home> Latest Articles> How to convert every character in a string to uppercase through str_split function in PHP combined with array_map?

How to convert every character in a string to uppercase through str_split function in PHP combined with array_map?

M66 2025-05-18

In PHP, str_split and array_map are two very practical functions that can be used in combination to help us process strings character-by-character. This article will explain how to convert each character in a string to uppercase through these two functions.

1. Understand str_split function

The function of the str_split function is to split a string into an array, and each character becomes an element in the array. Its basic usage is as follows:

$input = "hello";
$result = str_split($input);
print_r($result);

The output will be:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

As shown above, the str_split function splits the string "hello" into an array, and each character becomes a separate element of the array.

2. Understand the array_map function

array_map is a built-in function in PHP that applies a callback function to each element in an array and returns a new array. The basic usage of array_map is as follows:

$array = [1, 2, 3];
$result = array_map(function($item) {
    return $item * 2;
}, $array);
print_r($result);

The output result is:

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)

array_map applies the callback function to each element in the array and returns the new array.

3. Convert each character in the string to uppercase

To convert each character in a string to uppercase, we can use the str_split and array_map functions in combination. First use str_split to split the string into an array of single characters, and then use the array_map function to apply the strtoupper function to each character, thus converting it to uppercase. The code is as follows:

<?php
// Enter string
$input = "hello world";

// use str_split Split a string into an array of single characters
$chars = str_split($input);

// use array_map Convert each character to uppercase
$upperChars = array_map('strtoupper', $chars);

// use implode Recombining the uppercase character array into a string
$upperString = implode('', $upperChars);

// Output result
echo $upperString; // Output "HELLO WORLD"
?>

4. Code parsing

  • str_split($input) : Split the string $input into a character array.

  • array_map('strtoupper', $chars) : Apply the strtoupper function to each character in the $chars array, converting it to uppercase letters.

  • implode('', $upperChars) : recombines the converted uppercase character array into a string.

5. Complete code example

Here is a complete PHP code example:

<?php
// Enter string
$input = "hello world";

// use str_split Split a string into an array of single characters
$chars = str_split($input);

// use array_map Convert each character to uppercase
$upperChars = array_map('strtoupper', $chars);

// use implode Recombining the uppercase character array into a string
$upperString = implode('', $upperChars);

// Output result
echo $upperString; // Output "HELLO WORLD"
?>

6. Summary

By combining str_split and array_map functions, we can easily convert each character in a string to uppercase. This approach is very suitable for handling strings that require character conversion, especially when additional processing is required for each character in the string.