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
str_split was designed for just that.
$str = "sdasdasdsdkjsdkdjbskdbvksdbfksjdbfkdbfksdjbf";
$parts = str_split($str, 6);
print_r($parts);
You want chunk_split()
Not the cleanest way but it works.
<?php
$MyString = 'asdfasdlkfjasdlkfjasdlkfjasldkfj';
$MyNewString;
$n = 6; // How many you want before seperation
$MyNewString = substr($MyString,0,$n);
$i = $n;
while ($i < strlen($MyString)) {
$MyNewString .= '-'; // Seperator Character
$MyNewString .= substr($MyString,$i,$n);
$i = $i + $n;
}
echo $MyNewString
?>
$str = 'abcdefghijklmnopqrstuvwxyz';
$i = 0; $len = 6; $bits = array();
while($i < strlen($str)) {
$bits[] = substr($str, $i, $len);
$i += $len;
}
Edit: or use str_split as suggested (php has too many functions built in ffs!)
See the docs on str_split
.