PHP array and string conversion

Source: Internet
Author: User
Conversion of strings and arrays is often used in program development. PHP mainly uses the explode () function and implode () function to implement it. The following describes in detail. PHP array and string conversion

Conversion of strings and arrays is often used in program development. PHP mainly uses the explode () function and implode () function to implement it. The following describes in detail.

1. use the explode () function to convert a string to an array.

The explode () function is used to cut the string according to the specified string or character (separator.

Syntax format;

array explode(string separator,string string [,int limit])

Returns an array composed of strings. each array element is a substring of a specified string, which is separated by the string separator as a boundary point. If the limit parameter is set, the returned array contains up to limit elements, and the last element contains the rest of the string. if the separator is a null string (""), explode () the function returns false. if the separator value cannot be found in the string, The explode () function returns an array containing a single string element. if the limit parameter is negative, all elements except the last limit element are returned.

The following example uses the explode () function to separate "PHP, Chinese, and net" strings by ",". the sample code is as follows:

 

Output result:

When developing a voting management system, you often need to add the voting option to the voting system in the background as the voting content. The following uses The explode () function to differentiate the added voting options through "*", and then uses the white loop statement to output the added voting options.

The development procedure is as follows:

(1) use the development tool to create a PHP dynamic page and save it as index. php.

(2) use HTML to mark the design surface. First, create a voting form to add voting options. Then, use the each () function to extract the content in the global array $ _ POST, and finally use the while loop to output the voting option content. The code is as follows:

    
     
 

(3) add a table and add the following code to the cells of the table to output the added voting options.

 ';        echo $value."\n";    }}?>

(4) enter the address in the browser, press the Enter key, enter the content of the voting option, use "*" to separate the options, and click "submit". The running result is as follows:

2. use the implode () function to convert the array into a string.

The implode () function combines the content of the array into a new string.

The syntax format is as follows:

string implode(string glue,array pieces)

The glue parameter is a string type and refers to the delimiter to be imported. the piexes parameter is an array type and refers to the name of the array variable to be merged.

Use the implode () function to connect the content in the array with spaces as separators to form a new string. the sample code is as follows:

 

Output result:

[Recommended tutorials]

1. related topic recommendations: php Array (Array)

The above is the details of mutual conversion between PHP arrays and strings. For more information, see other related articles in the first PHP community!

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.