Using a PHP split string function to divide a string into an array

Source: Internet
Author: User
Tags explode php explode expression engine

Str_split () can split the string according to the required length, but if the string has UTF-8 encoded in Chinese, it will appear garbled.
Additional processing is required if you need to implement features that support Chinese characters that are separated by length

The code is as follows Copy Code

<?php
$str = ' Hello World ';
$arr = Str_split ($STR);
Print_r ($arr);
/*
Array
(
   
[0] => H
   
[1] => e
 &nb sp; 
[2] => l
   
[3] => l
   
[4] => o
 &n bsp; 
[5] =>
   
[6] => w
   
[7] => o
 &nb sp; 
[8] => R
   
[9] => l
   
[ten] => D
)
*/
 
$arr = Str_split ($str, 2);
Print_r ($arr);
/*
Array
(
   
[0] => He
   
[1] => ll
 & nbsp; 
[2] => o
   
[3] => wo
   
[4] => RL
&nbs p;  
[5] => D
)
 
*/

UTF-8 string function with length in Chinese for support of encoding

The code is as follows Copy Code

<?php
/**
* @version $Id: str_split.php 10381 2008-06-01 03:35:53z Pasamio $
* @package UTF8
* @subpackage Strings
*/
function Utf8_str_split ($str, $split _len = 1)
{
if (!preg_match ('/^[0-9]+$/', $split _len) | | $split _len < 1)
return FALSE;

$len = Mb_strlen ($str, ' UTF-8 ');
if ($len <= $split _len)
Return Array ($STR);

Preg_match_all ('/.{'. $split _len. '}| [^x00] {1, '. $split _len. '} $/us ', $str, $ar);

return $ar [0];
}

$str = ' Hello to you ';

$arr = Utf8_str_split ($STR);
Print_r ($arr);
/*
Array
(

[0] => H

[1] => E

[2] => L

[3] => L

[4] => O

[5] =>

[6] => you

[7] => Good
)
*/
?>

Primarily the function of the Chunk_split () function

The difference from Str_split () is that str_split () divides strings into groups by length, while Chunk_split () divides them into new strings by length with a specified special character.

Equivalent to the function of Str_split () +implode () two functions.

Similarly, this function does not support Chinese, and requires custom functions to implement such a function.

The code is as follows Copy Code

<?php
$data = "e0ab71ab9ed24e627a24e7d65367936393cb3b39db9a9e84d65cd7a9254a4665";
Echo Chunk_split ($data, "<br/>");
echo implode ("<br/>", Str_split ($data, 30));
Same as the chunk_split effect.
?>

Custom Chunk_split () functions that support Chinese

The code is as follows Copy Code

<?php
From Peter to dezzignz.com 05-apr-2010 11:30 @ php.net

function Mbstringtoarray ($STR) {
if (empty ($STR)) return false;
$len = Mb_strlen ($STR);
$array = Array ();
for ($i = 0; $i < $len; $i + +) {
$array [] = Mb_substr ($str, $i, 1);
}
return $array;
}

function Mb_chunk_split ($str, $len, $glue) {
if (empty ($STR)) return false;
$array = Mbstringtoarray ($STR);
$n = 0;
$new = ';
foreach ($array as $char) {
if ($n < $len) $new. = $char;
ElseIf ($n = = $len) {
$new. = $glue. $char;
$n = 0;
}
$n + +;
}
return $new;
}

$data = "E0ab Hello 4e627a24e7d65367936393cb3b39db9 Diaoyu dao a9e84d65cd7a9254a4665";
Echo Mb_chunk_split ($data, "<br/>");
?>

PHP Explode function Description , this function we often use

Explode function
The version supported by this function (PHP 4, PHP 5) is usually a faster alternative than split (). If you do not need the power of regular expressions, use explode () faster, so that you do not incur the waste of the regular expression engine.

explode-use a string to split another string

Description
Array explode (string $delimiter, string $string [, int $limit])
This function returns an array of strings, each of which is a substring of string, which is split by delimiter as a boundary point.

Parameters

Delimiter
The delimited character on the boundary.

String
The string entered.

Limit
If the limit parameter is set and is a positive number, the returned array contains up to limit elements, and the last element will contain the remainder of the string.

If the limit parameter is a negative number, all elements except the last-limit element are returned.

If the limit is 0, it will be treated as 1.

For historical reasons, although implode () can receive two parameter orders, explode () is not. You must ensure that the separator parameter is not preceded by a string parameter.

return value
This function returns an array of strings, each of which is a substring of string, which is split by delimiter as a boundary point.

If delimiter is an empty string (""), Explode () returns FALSE. If the value contained in delimiter is not found in string and a negative limit is used, an empty array is returned, or an array containing a single element of string is returned.

A bug
Update log

Version description
5.1.0 support for negative limit
4.0.1 increased the parameters limit
Example #1 explode () example
PHP code

The code is as follows Copy Code
<?php
Example 1
$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = Explode ("", $pizza);
echo $pieces [0]; Piece1
echo $pieces [1]; Piece2

Example 2
$data = "Foo:*:1023:1000::/home/foo:/bin/sh";
List ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);
Echo $user; Foo
Echo $pass; // *

?>

Example #2 explode () return examples
PHP code

The code is as follows Copy Code

<?php
/* A string that doesn ' t contain the delimiter would simply return A one-length array of the original string. */
$input 1 = "Hello";
$input 2 = "Hello,there";
Var_dump (Explode (', ', $input 1));
Var_dump (Explode (', ', $input 2));

?>


The above routines will output:

Array (1)
(
[0] => string (5) "Hello"
)
Array (2)
(
[0] => string (5) "Hello"
[1] => string (5) "There"
)

Examples of Example #3 limit parameters
PHP code

The code is as follows Copy Code
<?php
$str = ' One|two|three|four ';

Limit of positive numbers
Print_r (Explode (' | ', $STR, 2));

Negative limit (from PHP 5.1)
Print_r (Explode (' | ', $STR,-1));
?>

The above routines will output:
Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => t Hree
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.