silkfire Posted April 8, 2011 Share Posted April 8, 2011 What's the difference between str_split() and chunk_split()? They seem to perform the same task? Quote Link to comment https://forums.phpfreaks.com/topic/233075-difference-in-the-split-functions/ Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 str_split splits a string into an array every n characters. chunk_split breaks up a string by inserting a line break every n characters (and returns a string). Quote Link to comment https://forums.phpfreaks.com/topic/233075-difference-in-the-split-functions/#findComment-1198679 Share on other sites More sharing options...
nethnet Posted April 8, 2011 Share Posted April 8, 2011 They are similar, but have different outputs and default split lengths. str_split() returns an array and defaults to a split being made every character. chunk_split() returns a string with \r\n placed every 76 characters. For both functions, the split length can be changed with an optional 2nd parameter. In essence, str_split is useful for exploding strings based on a chunk length as opposed to a needle with explode(). chunk_split() is primarily useful for formatting certain long strings to meet RFC semantics. This is mostly for hash values and encryption methods that potentially produce strings longer than 76 characters. Quote Link to comment https://forums.phpfreaks.com/topic/233075-difference-in-the-split-functions/#findComment-1198680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.