|
|
|
Converts a string to an array. If the optional $num parameter is specified, the returned array will be broken down into chunks with each being split in $num length.
This fuction is almost the same as the fuction str_split included in php5.
|
<?php
function strsplt($thetext,$num)
{
if (!$num)
{
$num=1;
}
$arr=array();
$x=floor(strlen($thetext)/$num);
while ($i<=$x)
{
$y=substr($thetext,$j,$num);
if ($y)
{
array_push($arr,$y);
}
$i++;
$j=$j+$num;
}
return $arr;
}
?> | |
Usage Example
| <?php
$str = "Hello Friend";
$arr1 = strsplt($str,1);
$arr2 = strsplt($str, 3);
print_r($arr1);
print_r($arr2);
?> | |
Output may look like:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
|
|
| columned txt file to array()? Categories : Arrays, Strings, Regexps, PHP | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | Function to create a separated list Categories : PHP, Arrays, Strings | | | How to ifconfig down/up a list of IP's Categories : Arrays, Strings, Filesystem, PHP | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | WWW interface to Unix Manual(phpMan)
Categories : Program Execution, Strings, Arrays, PHP | | | Get TemplateMonster data Categories : Arrays, Ecommerce, PHP, Strings | | | Can the word DO be used in arrays? Categories : Arrays, PHP, Strings | | | Parse string to find sub-string between two arbitrary strings Categories : PHP, Strings, HTML and PHP, Arrays | | | Grab images from one or more URLs and save them to a specified local directory. Categories : PHP, Filesystem, Strings, Arrays | | | How to Get a character array from a string Categories : PHP, Strings, Arrays | | | Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP | | | This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change Categories : PHP, Arrays, Cookies, MySQL, Databases | | | What is the best way to split a string that consists of two bits of data
seperated by whitespace? Categories : Regexps, Strings, PHP | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | |
| | | | matthew waygood wrote : 1141
a bit long winded, can anyone suggest a use for this?
function strsplt($thetext,$step=1)
{
$arr=array();
if( (is_numeric($step)) && ($step>0) )
{
for($iterator=0;$iterator<strlen($thetext);$iterator=$iterator+$step)
{
$arr[]=substr($thetext, $iterator, $step);
}
}
return $arr;
}
| | | | Abdoulaye Siby wrote : 1142
Hello,
This function already does exist in PHP.
http://www.php.net/manual/en/function.str-split.php
str_split
(PHP 5)
str_split -- Convert a string to an array
Description
array str_split ( string string [, int split_length])
Converts a string to an array. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
Example 1. Example uses of str_split()
<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
Output may look like:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
| | | | ROMEL CUZON wrote :1143
Wondering about the error appear when I migrate my sample program from home to office . This function I made was derive after I realize that it was not included in PHP4 that is in our office. I use PHP5 at home. I think the first sample comment above is the shortcut function.
| |
|
|