PHP arrays and strings convert to each other

Source: Internet
Author: User
PHP Arrays and strings convert to each other

The conversion of strings and arrays is often used during program development, and PHP uses the explode () function and the implode () function, which we explain in detail below.

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

The explode () function cuts the string by the specified string or character (separator).

The syntax format is as follows;

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

Returns an array of strings, each of which is a substring of the specified string, separated by the string separator as a boundary point. If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string, and if separator is an empty string (""), the Explode () function will return FALSE if separator contains Value is not found in string, the explode () function returns an array containing a single element of string, and if the parameter limit is a negative number, all elements except the last limit element are returned.

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

<?phpheader ("content-type:text/html; Charset=utf-8 "); $str =" PHP, Chinese, net ";      Define a string $strs = Explode (",", "$str"); Apply the explode () function to convert the string to an array of print_r ($strs);                Output array element?>

The output is:

When developing a voting management system, it is often necessary to add voting options to the voting system in the background as a voting content. The following uses the explode () function to differentiate the voting options that are added by "*", and then uses the white loop statement to output the added voting options separately from the polygon.

The specific development steps are as follows:

(1) Using the development tool, create a new PHP dynamic page, save as index.php.

(2) using HTML markup to design polygons, first create a poll form for implementing the Add voting option, and then use the each () function to extract the contents of the global array $_post, and eventually use the while loop to output the voting option content. The code is as follows:

<! DOCTYPE html>

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

<?phpif ($_post[submit]!= "") {    $content = $_post[content];    $data = Explode ("*", $content);    while (list ($name, $value) =each ($data)) {        echo ' <input type= ' checkbox ' name= ' checkbox ' value= ' checkbox ' > ';        echo $value. " \ n ";    }}? >

(4) Enter the address in the browser, press ENTER, enter the content of the voting options, use "*" to split between the options, click "Submit" and run the results as follows

2. Use the implode () function to convert an array to a string

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

The syntax format is as follows:

String implode (string Glue,array pieces)

The parameter glue is the string type, which is the delimiter to pass in, and the parameter piexes is the array type, which refers to the array variable name of the incoming element to be merged.

Use the implode () function to concatenate the contents of the array into a new string by separating them with spaces, as shown in the following example code:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $str =array (" PHP "," Chinese "," net "," url "," www.php.cn "); Echo implode (" ", $str)  ; Combines the elements of an array into a new string with a space as a delimiter?>

The output is:

"Related tutorials Recommended"

1. Related topics: "PHP array"

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.