Home > Backend Development > PHP Problem > php string to array

php string to array

藏色散人
Release: 2023-02-24 07:40:01
Original
4946 people have browsed it

php string to array

php string to array

str_split

(PHP 5, PHP 7)

str_split — Convert a string to an array

Description

str_split ( string $string [, int $split_length = 1 ] ) : array
Copy after login

Convert a string to an array.

Parameters

string: Input string.

split_length: The length of each segment.

Return value

If the optional split_length parameter is specified, each element in the returned array is a character block of length split_length, otherwise each character block is a single character.

If split_length is less than 1, return FALSE. If the split_length argument exceeds the length of string , the entire string will be returned as only one element of the array.

Example

Example #1 str_split() Usage Example

<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
Copy after login

The above routine will output:

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
)
Copy after login

Comments

Note:

When dealing with multi-byte characters, str_split() will convert by the number of bytes, not the number of characters.

Related recommendations: "PHP Tutorial"

The above is the detailed content of php string to array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template