Page 1 of 1
Splitting a string
Posted: Sat Jan 06, 2007 2:53 am
by impulse()
I should probably know the answer to this, but it's not apparent at the moment.
I I had a string like this - "hellomynameisstephen". How would it be possible to split it by each letter separately? So there are as many elements as there are letters.
Regards,
Posted: Sat Jan 06, 2007 3:37 am
by Kieran Huggins
Code: Select all
$array = str_split('hellomynameisstephen');
Only works in php5... I'm curious to find a php4 solution, will keep looking.
Posted: Sat Jan 06, 2007 4:10 am
by Kieran Huggins
Here's a php4 version of str_split():
Code: Select all
function str_split($str) {
$output = array();
for($i=0;$i<strlen($str);$i++){
$output[] = $str{$i};
}
return $output;
}
Posted: Sat Jan 06, 2007 5:05 am
by Ollie Saunders
Is
possibly a better php 4 solution.
Posted: Sat Jan 06, 2007 6:23 am
by brendandonhue
You can access a string as if it were an array in PHP, if that works for what you're trying to do.
Posted: Sat Jan 06, 2007 6:32 am
by impulse()
I needed to use a
statement. And that doesn't seem to recognise a string as an array nor does
so I was unable to define how many times the loop should occur.
Posted: Sat Jan 06, 2007 6:56 am
by brendandonhue
Code: Select all
<?php
$string = 'hellomynameisstephen';
$length = strlen($string);
for($i = 0; $i < $length; $i++)
{
echo $string[$i] . '<br>';
}
?>
Posted: Sat Jan 06, 2007 7:02 am
by Kieran Huggins
ole wrote:Is
possibly a better php 4 solution.
Looked at that one, outputs a string
You could just include the function wrapped in a condition:
Code: Select all
if(!function_exists('str_split')){
function str_split($str) {
$output = array();
for($i=0;$i<strlen($str);$i++){
$output[] = $str{$i};
}
return $output;
}
}
That way it will be defined as a replacement for the native php5 str_split() if it doesn't already exist.
Posted: Sun Jan 07, 2007 12:21 pm
by Ollie Saunders
Looked at that one, outputs a string
Ahh sorry. I didn't test it. Although I will now because I'm puzzling over exactly what that function is supposed to do.
Posted: Sun Jan 07, 2007 2:13 pm
by Ollie Saunders
OK here is the most comprehensive replacement for str_split() I could come up with:
Code: Select all
if (!function_exists('str_split')) {
/**
* PHP 4 equivlent of str_split.
*
* @link http://php.net/manual/en/reference.pcre ... ifiers.php
* for utf8 availability
* @param string $string
* @param int $length
* @param bool $utf8Support
* @return array
*/
function str_split($string, $length = 1, $utf8Support = false)
{
if (!$length || !is_int($length)) {
return $string;
}
$pattern = '~(.{' . $length. '})~s' . ($utf8Support ? 'u' : '');
return array_values(array_filter(
preg_split($pattern, $string, null, PREG_SPLIT_DELIM_CAPTURE)
));
}
}
function test($actual, $expected)
{
if ($actual == $expected) {
return;
}
echo '<pre>', var_dump($actual, true), '</pre><br />';
}
test(str_split('foo'), array('f', 'o', 'o'));
test(str_split("\r\n\t"), array("\r", "\n", "\t"));
test(str_split('foo', 2), array('fo', 'o'));
the array_values(array_filter( is necessary because preg_split returns a load of empty elements and the PREG_SPLIT_NO_EMPTY flag causes it to return nothing. I'm interested if anyone has a better solution to that. I haven't tested performance of this but it not impossible it will out perform the for loop version.
and for anyone who is wondering chunk_split pads adds spaces (or other char) every nth char.
Posted: Sun Jan 07, 2007 2:15 pm
by Kieran Huggins
ole wrote:Looked at that one, outputs a string
Ahh sorry. I didn't test it. Although I will now because I'm puzzling over exactly what that function is supposed to do.
It will inject a character into a string after so many characters... it's often used to add line breaks (\r\n) to a long string, like base64'd data in an email.
Posted: Sun Jan 07, 2007 5:02 pm
by feyd
You know, there is
str_split() in the PEAR compatibility library.
http://pear.php.net/package/PHP_Compat
Posted: Sun Jan 07, 2007 5:56 pm
by Ollie Saunders
You know, there is str_split() in the PEAR compatibility library.
Why didn't you say so before! Tiiskk Honestly.
OK there's is better
Code: Select all
if (!function_exists('str_split')) {
function str_split($string, $split_length = 1)
{
if (!is_scalar($split_length)) {
user_error('str_split() expects parameter 2 to be long, ' .
gettype($split_length) . ' given', E_USER_WARNING);
return false;
}
$split_length = (int) $split_length;
if ($split_length < 1) {
user_error('str_split() The length of each segment must be greater than zero', E_USER_WARNING);
return false;
}
// Select split method
if ($split_length < 65536) {
// Faster, but only works for less than 2^16
preg_match_all('/.{1,' . $split_length . '}/s', $string, $matches);
return $matches[0];
} else {
// Required due to preg limitations
$arr = array();
$idx = 0;
$pos = 0;
$len = strlen($string);
while ($len > 0) {
$blk = ($len < $split_length) ? $len : $split_length;
$arr[$idx++] = substr($string, $pos, $blk);
$pos += $blk;
$len -= $blk;
}
return $arr;
}
}
}
They used preg_match_all.