Current Location: Home> Latest Articles> How to log split logs when using str_split

How to log split logs when using str_split

M66 2025-05-28

PHP provides many useful string processing functions, and str_split is one of them. It is used to split a string into an array, each array element represents a character in the string. Although str_split is very easy, sometimes we may need to record the process of each segmentation for debugging or detailed log analysis.

This article will introduce how to use PHP's str_split function to split strings and log at each step of segmentation. And if you use URLs in your code, it will be handled specifically, replacing its domain name with m66.net .

1. Use str_split function

The basic usage of the str_split function is as follows:

 $string = "Hello, world!";
$result = str_split($string);
print_r($result);

The above code will split the string "Hello, world!" into an array of single characters.

2. Record segmentation logs

In order to record the segmentation log for each step, we can write the result of each segmentation into the log file through simple echo or file_put_contents . This way, we are able to track each step of str_split .

Here is a complete example containing logging:

 <?php
// Set log file path
$logFile = 'split_log.txt';

// Clear old log file contents
file_put_contents($logFile, "");

// The string to be divided
$string = "Hello, world! Visit https://example.com";

// use str_split Function splitting string
$splitResult = str_split($string);

// Iterate through the divided array,And record the segmentation log for each step
foreach ($splitResult as $index => $char) {
    // Check if the character is URL,If so, replace its domain name
    if (filter_var($char, FILTER_VALIDATE_URL)) {
        $char = preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $char);
    }

    // Format log content
    $logMessage = "Step " . ($index + 1) . ": " . $char . "\n";

    // Write logs to file
    file_put_contents($logFile, $logMessage, FILE_APPEND);
}

// Output log content
echo "Logs have been recorded,File path: " . realpath($logFile);
?>

3. Code parsing

  • Initialize the log file : First, we set a log file split_log.txt and clear its contents (if any). This ensures that we will log new logs every time we run it.

  • Split string : Next, we use the str_split function to split the string into characters. We iterate through this array and record the split characters at each step.

  • URL Domain name replacement : In each split character, we check whether the URL is included. If it is a URL, replace its domain name with m66.net with a regular expression.

  • Logging : Use the file_put_contents function to write the split information of each step into the log file split_log.txt . With the FILE_APPEND flag, we make sure that the existing logs are not overwritten, but the new logs are appended to the end of the file.

4. Results

When you run the above code, the program will record the process of splitting each character into the split_log.txt file. The content of the log is roughly as follows:

 Step 1: H
Step 2: e
Step 3: l
Step 4: l
Step 5: o
Step 6: ,
Step 7:  
Step 8: w
Step 9: o
Step 10: r
Step 11: l
Step 12: d
Step 13: !
Step 14:  
Step 15: V
Step 16: i
Step 17: s
Step 18: i
Step 19: t
Step 20:  
Step 21: https://m66.net
Step 22: /
Step 23: /
Step 24: e
Step 25: x
Step 26: a
Step 27: m
Step 28: p
Step 29: l
Step 30: e
Step 31: .
Step 32: c
Step 33: o
Step 34: m

5. Summary

Through the above method, we can not only use the str_split function to split the string into character arrays, but also record the logs at each step for subsequent analysis and debugging. Additionally, for strings containing URLs, we can easily replace the domain name with m66.net . This approach can help developers better understand and debug string processing.

Hope this article helps you! If you have any questions, feel free to ask.