|
użytkowników online: 57
|
OPINIE UŻYTKOWNIKÓW
|
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!
Kamil Dmowski
Polski Czerwony Krzyż
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
str_split (PHP 5) str_split --
Convert a string to an array
Descriptionarray 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.
Przykład 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
) |
|
Przykład 2. Examples related to str_split() |
<?php
$str = "Hello Friend";
echo $str{0}; echo $str{8}; $arr1 = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
?>
|
|
See also chunk_split(),
preg_split(),
split(),
count_chars(),
str_word_count(), and
for.
User Contributed Notesfrj_php dot net at thederea dot com
01-Feb-2006 06:47
another solution:
if ( !function_exists('str_split') ) {
function str_split($str) {
return preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
}
}
[please delete my previous code, there was a typo]
minirop at peyj dot com
29-Jan-2006 09:06
An easy one for php4 :
<?php
if (!function_exists('str_split')){
function str_split($str) {
return explode('|',wordwrap ($str,1,'|',1));
}
}
?>
02-Dec-2005 03:21
Another piece of code to replicate str_split() behaviour in PHP 4. No crazy explodes, unpacking or regular expressions.
<?php
if (!function_exists('str_split')){
function str_split($string, $split_length=1){
if ($split_length < 1){
return false;
}
for ($pos=0, $chunks = array(); $pos < strlen($string); $pos+=$split_length){
$chunks[] = substr($string, $pos, $split_length);
}
return $chunks;
}
}
?>
neos2k at gmx dot net
10-Nov-2005 05:23
Note that Ludvig Ericson's code:
<?php
if (!function_exists('str_split')) {
function str_split($string, $split_length = 1) {
return explode("\r\n", chunk_split($string, $split_length));
}
}
?>
...will return an empty item at the end of the array, which can be confusing (i.e. when using foreach).
28-Oct-2005 05:59
Lazy man's replacement:
<?php
preg_split('/(.{'.$length.'})/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
?>
ludvig dot ericson at gmail dot com
16-Oct-2005 10:34
It seems that most the str_split imitations around here are pretty weird.
chunk_split can be used for the exact same purpose:
<?php
if (!function_exists('str_split')) {
function str_split($string, $split_length = 1) {
return explode("\r\n", chunk_split($string, $split_length));
}
}
?>
I would not say that it is beautiful, yet it is not as insane as that unpack thing down a few comments.
You can probably improve it by checking $split_length and such, but this Just works (tm) and I doubt that you ever call it with a negative $split_length anyway... Unless you are insane of course.
P dot BENVENISTE info at pommef dot com
21-Sep-2005 03:22
str_split is a PHP 5 Function;
Here is the PHP 4 version
if(!function_exists('str_split')){
function str_split($str,$split_lengt=1){
$cnt = strlen($str);
for ($i=0;$i<$cnt;$i+=$split_lengt)
$rslt[]= substr($str,$i,$split_lengt);
return $rslt;
}
}
jbricci at ya-right dot com
09-Sep-2005 07:13
Here a way to do it using * unpack() *
1. get the size of the string.
2. divide the string length by the character length to split the string at.
3. then build the unpack() to do the multi sub string.
4. use array_combine (because unpack() starts at array element 1)
5. return the split string as a array!
<?
$text = 'The cat in the hat. Green eggs and ham. Sam I am.';
$split = 20;
$out = split_it ( $text, $split );
print_r ( $out );
function split_it ( $str, $max )
{
$r = strlen ( $str );
if ( $r <= $max )
{
return ( array ( $str ) );
}
$s = array ( 0 );
$t = '';
$u = $r / $max;
$v = '';
if ( strpos ( $u, '.' ) !== false )
{
$p = explode ( '.', $u );
$u = $p[0];
$v = 'A' . '* ' . ( $u + 1 ) . '/';
}
for ( $i = 1; $i <= $u; $i++ )
{
$t .= 'A' . $max . ' ' . $i . '/';
$s[] = $i;
}
$t .= $v;
return ( array_combine ( $s, unpack ( $t, $str ) ) );
}
?>
1413 at blargh dot com
20-Aug-2005 12:06
If you just need to split strings, and not depend on any other side effects or the like, I have the following in my standard functions file that I use for both PHP4 and PHP5 projects:
if(!function_exists('str_split'))
{
function str_split($str, $len = 1)
{
return(explode("\1\2\3",chunk_split($str, $len, "\1\2\3")));
}
}
Jason <support [at] ejdude [dot] com>
08-Aug-2005 10:24
My version of str_split() for PHP < 5:
<?php
if ( !function_exists('str_split') )
{
function str_split($string, $split_length = 1)
{
$array = array();
$strlen = strlen($string);
if ( $split_length < 1 )
{
return false;
}
if ( $split_length >= $strlen )
{
return array($string);
}
for ( $i = 0, $key = 0, $count = 0; $i < $strlen; $i++, $count++ )
{
if ( $count >= $split_length )
{
$count = 0;
$key++;
}
if ( isset($array[$key]) )
{
$array[$key] .= $string{$i};
}
else
{
$array[$key] = $string{$i};
}
}
return $array;
}
}
?>
LeChuck from <bfpdevel gmail dot com>
15-Jul-2005 05:05
This is some modification to funcion of ing_rivera.
Now you can use
splitstring($string);
splitstring($string, $length);
<?php
function splitstring($string,$len=0)
{
if($len != 0)
{
for($i=0;$i<ceil(strlen($string)/$len);$i++)
$rtnarr[$i]=substr($string, $len*$i, $len);
}
else
{
for($i=0;$i<strlen($string);$i++)
$rtnarr[$i]=substr($string, $i, 1);
}
return($rtnarr);
}
?>
ing_rivera at yahoo dot com
25-Jun-2005 09:55
str_split For PHP versions < 5.0
///////////////////////////////////////////////////////////////
//Converts a string to an array. The returned array will be
//broken down into chunks with each being $len in length
///////////////////////////////////////////////////////////////
function splitstring($string,$len) {
for($i=0;$i<ceil(strlen($string)/$len);$i++)
$rtnarr[$i]=substr($string, $len*$i, $len);
return($rtnarr);
}
slgundam at gmail dot com
12-Jun-2005 08:11
i found this to be the best solution for emulation of the str_split function
if (!function_exists('str_split')){
function str_split($string, $max_length = 1){
for($i = 0, $cur_length = 0, $cur_array = 0, $spl_string = array(0 => ''); isset($string{$i}); $i++, $cur_length++){
if ($cur_length >= $max_length){
$cur_length = 0;
$cur_array++;
$spl_string[$cur_array] = $string{$i};
}
else{
$spl_string[$cur_array] .= $string{$i};
}
}
return($spl_string);
}
}
Lepidosteus
04-Jun-2005 06:35
katoda's function seems to loose part of the string while splitting.
i.e a md5 string splitted into 4 chars lenght strings will lost half its content (32 chars => 16 chars).
Here's a working one (made by nic58) :
<?php
function split_str($str, $num = 1)
{
$arr = array();
for($i = 0, $len = strlen($str), $j = -1; $i < $len; $i++)
{
if(($i % $num) == 0)
{
$j++;
}
$arr[$j] .= $str[$i];
}
return $arr;
}
?>
katoda at vp dot pl
31-May-2005 10:29
Definition:
function str_split($str, $num=1)
{
for ($i=0; $i<($z=(strlen($str)/$num)); $i++)
{
$arr[$i]=substr($str, 0, $num);
$str=substr($str, $num);
}
return $arr;
}
organek at hektor dot umcs dot lublin dot pl
21-May-2005 01:57
This is a little function to split a string into shorter strings with max lenght $n in such way, that it don't split words (it search for spaces), it's usefull for articles or sth.
Result is put in $ttab variable, and function result is number of "pages".
<?php
function divide_text($text, $n, &$ttab) {
$ttab = array();
$l = strlen($text); $cb = 0; $p = 0; if ($l <= $n) {
$ttab[0] = $text;
return 1;
} else {
$ctrl = 1;
while(((($p-1) * $n) < $l) && ($ctrl < 100)) {
$crtl++; $tmp = substr($text, $cb, $n);
$lastpos = strrpos($tmp," ");
if ( (is_bool($lastbool) && !$lastpos) || ( $l - $cb <= $n)) {
$ttab[$p] = $tmp;
} else {
$tmpgood = trim(substr($tmp, 0,$lastpos)); $ttab[$p] = $tmpgood;
$cb += $lastpos + 1 ;
}; $p++;
}; return $p;
}; } ?>
shugotenshi at gmail dot com
20-Apr-2005 04:58
It's best not to overcomplicate things with redundant PHP-side loops and start taking into consideration of preg_split() to simulate str_split()'s behavior for < PHP 5 versions. http://www.php.net/preg-split has an example of how to split strings by one character, but with a little regex knowledge you can easily use a quantifier to split by as many characters as you want.
aidan at php dot net
20-May-2004 06:53
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
|