WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Convert a string to an array
Categories : PHP, Arrays, Strings
ROMEL CUZON
Date : Jul 09th 2004
Grade : 4 of 5 (graded 4 times)
Viewed : 24550
File : No file for this code example.
Images : No Images for this code example.
Search : More code by ROMEL CUZON
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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&gt;0) )
    {
        for($iterator=0;$iterator&lt;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()
&lt;?php

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?&gt;

Output may look like:

Array
(
    [0] =&gt; H
    [1] =&gt; e
    [2] =&gt; l
    [3] =&gt; l
    [4] =&gt; o
    [5] =&gt;
    [6] =&gt; F
    [7] =&gt; r
    [8] =&gt; i
    [9] =&gt; e
    [10] =&gt; n
    [11] =&gt; d
)

Array
(
    [0] =&gt; Hel
    [1] =&gt; lo 
    [2] =&gt; Fri
    [3] =&gt; 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.