|
|
|
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
)
|
|
| Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP | | | columned txt file to array()? Categories : Arrays, Strings, Regexps, PHP | | | How to ifconfig down/up a list of IP's Categories : Arrays, Strings, Filesystem, PHP | | | Grab images from one or more URLs and save them to a specified local directory. Categories : PHP, Filesystem, Strings, Arrays | | | 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 | | | 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 | | | How to Get a character array from a string Categories : PHP, Strings, Arrays | | | Can the word DO be used in arrays? Categories : Arrays, PHP, Strings | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | clearing variables in php3 Categories : Variables, Arrays, PHP | |
| | | | 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.
| |
|
|
|