Home > Article > Backend Development > How can I split a string by multiple spaces or tabs in PHP?
Dividing a string into an array based on specific delimiters is often necessary in programming. In this case, we want to explode a string by one or more occurrences of spaces or tabs.
The solution to this task lies in utilizing the preg_split() function:
$str = "A B C D"; $delimiter = "/\s+/"; $parts = preg_split($delimiter, $str);
Here's what each part of this code accomplishes:
The final result is an array $parts that contains the individual string elements: ["A", "B", "C", "D"]. This approach ensures that the string is split at every occurrence of one or more consecutive spaces or tabs, providing a flexible and efficient solution.
The above is the detailed content of How can I split a string by multiple spaces or tabs in PHP?. For more information, please follow other related articles on the PHP Chinese website!