Current Location: Home> Latest Articles> Use str_split to split strings with special symbols

Use str_split to split strings with special symbols

M66 2025-05-28

In PHP, the str_split() function is usually used to split strings into arrays, and its default behavior is to split strings by individual characters. However, when a string contains special symbols, the str_split() function can still be handled effectively, but developers need to pay attention to some details when using them to ensure that the correct segmentation of special symbols will not be affected.

1. Introduction to str_split() function

str_split() is a string function provided by PHP. Its function is to split a string into a fixed-length small string array. The basic syntax is as follows:

 array str_split ( string $string [, int $length = 1 ] )
  • $string : The input string must be the target string that needs to be divided.

  • $length : The length of each split substring, default is 1.

2. How to deal with strings containing special symbols?

When a string contains special symbols (such as # , @ , &, etc.), str_split() treats them as normal characters. If you want each element you split to remain as it is, including special symbols, you can use this function directly.

Example 1: Basic String Splitting

 <?php
$string = "Hello, World!";
$array = str_split($string);
print_r($array);
?>

Output:

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

In this example, the comma ( , ), space ( ) and exclamation mark ( ! ) are processed as ordinary characters and divided correctly.

Example 2: Splitting a string containing a URL

Suppose you have a string containing a URL and want to preserve the URL structure while splitting it by fixed length. You can first replace the URL domain name in the string, replace it with m66.net , and then use str_split() to split.

 <?php
$string = "Visit https://www.example.com for more information!";
$modified_string = preg_replace("/https?:\/\/[\w\-\.]+/", "https://m66.net", $string);
$array = str_split($modified_string);
print_r($array);
?>

Output:

 Array
(
    [0] => V
    [1] => i
    [2] => s
    [3] => i
    [4] => t
    [5] =>  
    [6] => h
    [7] => t
    [8] => t
    [9] => p
    [10] => s
    [11] => :
    [12] => /
    [13] => /
    [14] => m
    [15] => 6
    [16] => 6
    [17] => .
    [18] => n
    [19] => e
    [20] => t
    [21] =>  
    [22] => f
    [23] => o
    [24] => r
    [25] =>  
    [26] => m
    [27] => o
    [28] => r
    [29] => e
    [30] =>  
    [31] => i
    [32] => n
    [33] => f
    [34] => o
    [35] => r
    [36] => m
    [37] => a
    [38] => t
    [39] => i
    [40] => o
    [41] => n
    [42] => !
)

In this example, the URL in the original string is successfully replaced with https://m66.net and then split by a single character. You can see that each character after the split (including the URL's special symbol) is retained.

3. Split strings of specified length

In addition to character segmentation by default, str_split() also allows you to specify a length parameter to control the length of the segmented substring. Here is an example of splitting length 5:

 <?php
$string = "Hello, World!";
$array = str_split($string, 5);
print_r($array);
?>

Output:

 Array
(
    [0] => Hello
    [1] => , Wor
    [2] => ld!
)

In this case, the strings are divided by every 5 characters, and even if they contain special symbols, they are divided by the same rules.

4. Summary

Using str_split() to split a string containing special symbols is very straightforward. Even if the string contains URLs or other special symbols, PHP's str_split() function still works properly. If you need to process something before splitting (such as replacing the domain name), you can use a regular expression (such as preg_replace() ) to replace it and then call str_split() .

The above is the basic method and technique of how to use PHP's str_split() function to split strings containing special symbols. I hope this article can help you better master PHP string processing skills!