Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into equal parts using PHP

Tags:

string

php

I'd like to get a string, for example 'sdasdasdsdkjsdkdjbskdbvksdbfksjdbfkdbfksdjbf' and split that up every six charaters.

I don't think explode or strtok will work for that?

Any ideas?

like image 428
logic-unit Avatar asked Nov 25 '09 00:11

logic-unit


People also ask

How can I split a string into two parts in PHP?

PHP | explode() Function explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs.

What is Chunk_split in PHP?

The chunk_split() function splits a string into a series of smaller parts. Note: This function does not alter the original string.

How do I separate comma separated values in PHP?

The task is to split the given string with comma delimiter and store the result in an array. Use explode() or preg_split() function to split the string in php with given delimiter. PHP | explode() Function: The explode() function is an inbuilt function in PHP which is used to split a string in different strings.

What PHP function would you use to join array elements into a string using a separator string )?

PHP | join() Function The join() function is built-in function in PHP and is used to join an array of elements which are separated by a string.


1 Answers

str_split was designed for just that.

$str = "sdasdasdsdkjsdkdjbskdbvksdbfksjdbfkdbfksdjbf";
$parts = str_split($str, 6);
print_r($parts);
like image 63
Marek Karbarz Avatar answered Oct 15 '22 03:10

Marek Karbarz