PHP array and string conversion function _php Tutorial

Source: Internet
Author: User
In PHP we are going to convert the string in the array can use the function has Str_split (), Explode (), preg_split () function, if the array is converted to a string we also have a function implode () function with directly to the array connected together.

Let's start with a string conversion array.

Str_split ()

Print_r (Str_split ("Hello"));
?>

Array
(
[0] = H
[1] = = E
[2] = l
[3] = L
[4] = O
)

Explode ()

$STR = "Hello World". It ' s a beautiful day. ";
Print_r (Explode ("", $str));
?>

Results

Array
(
[0] = = Hello
[1] = world.
[2] + = It ' s
[3] = a
[4] = Beautiful
[5] = = Day.
)

Preg_split () function

The code is as follows Copy Code

$user _info = "+j+++g+++++w";
$fields = Preg_split ("/+{1,}/", $user _info);
while ($x < sizeof ($fields)):
Print $fields [$x]. "
";
$x + +;
Endwhile;
?>

Summary in PHP str_split (), explode () function is the same, this is not introduced.

Converts an array to a string

Implode ()


Cases

The code is as follows Copy Code

$array = Array (' A ', ' B ', ' C ');

echo implode ($array);

Results ABC


http://www.bkjia.com/PHPjc/445612.html www.bkjia.com true http://www.bkjia.com/PHPjc/445612.html techarticle In PHP We are going to convert the string in the array can use the function has Str_split (), Explode (), preg_split () function, if the array is converted to a string we also have a function implode () function ...

  • Related Article

    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.