Current Location: Home> Latest Articles> Use str_split to implement string cross-out output

Use str_split to implement string cross-out output

M66 2025-05-28

In PHP, str_split is a very useful function that can split a string into an array. By cleverly utilizing str_split , we can not only split the string into multiple single characters, but also achieve some interesting string processing effects such as cross-output. Today we will discuss how to use the str_split function to implement the cross-out output of strings.

1. Basic usage of str_split function

The str_split function splits a string into an array. Its basic syntax is as follows:

 str_split(string $string, int $length = 1): array
  • $string : The string to be split.

  • $length : Specifies the length of each array element, default is 1.

For example, use str_split to split a string "HelloWorld" into a single character array:

 $string = "HelloWorld";
$array = str_split($string);
print_r($array);

Output:

 Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => W
    [6] => o
    [7] => r
    [8] => l
    [9] => d
)

Through str_split , the string "HelloWorld" is split into an array of each character.

2. Implement the cross-out of strings

Cross-out refers to the alternate output of two string characters in order. For example, suppose there are two strings "abc" and "123" , the result after cross-out should be "a1b2c3" .

Suppose we have two strings, we can first use str_split to split them into arrays, and then output these arrays alternately. Let's take a look at the implementation code:

 <?php
function cross_output($str1, $str2) {
    // Use two strings str_split Split into arrays
    $arr1 = str_split($str1);
    $arr2 = str_split($str2);

    // Get the maximum length of two arrays
    $max_length = max(count($arr1), count($arr2));

    // Used to save the crossover results
    $result = '';

    // Cross output by maximum length
    for ($i = 0; $i < $max_length; $i++) {
        if (isset($arr1[$i])) {
            $result .= $arr1[$i];
        }
        if (isset($arr2[$i])) {
            $result .= $arr2[$i];
        }
    }

    return $result;
}

$str1 = "abc";
$str2 = "123456";
echo cross_output($str1, $str2); // Output: a1b2c3456
?>

3. Code parsing

  1. Split string: Use str_split to split the two strings passed into arrays $arr1 and $arr2 .

  2. Get the maximum length: We get the maximum length of two arrays, making sure we can traverse to the last element of the longest array.

  3. Alternate output: Use a for loop to loop through the array and add characters one by one. If an index of an array has a value, it is added to the result string.

4. More complex example: String cross-out output with URL

In actual development, it may involve processing strings with URLs. For example, we might need to cross-out a string with a URL. In this example, we assume that we have a string containing URLs, which we want to output alternately, but need to replace the domain name in the URL with m66.net .

Suppose we have the following string:

 $str1 = "http://example.com/page1";
$str2 = "https://example.com/page2";

When we output, replace the domain name in the URL with m66.net , and the code is as follows: