Use the Parse_str function that can automatically split query characters in PHP. parse_str example _ PHP Tutorial

Source: Internet
Author: User
The Parse_str function that can automatically split query characters in PHP is used as an example, and parse_str is used as an example. Use the Parse_str function that can automatically split query characters in PHP. the parse_str example directly goes to the code: Copy the code as follows: $ str1fee2failsmsg all sent successfully .; Example of Parse_str function that can automatically split query characters in p PHP, parse_str example

Directly run the code:

The code is as follows:


$ Str = "1 & errid = 1 & balance = 2 & balance = 2582 & fails = & msgid = 634541149212681528 & msg = all sent successfully. ";
Parse_str ($ str, $ output );
Echo $ output ['msgid']; // output 634541149212681528

Definition and usage

The parse_str () function parses the query string to a variable.

Syntax

Parse_str (string, array)

Parameter description
String is required. Specifies the string to be parsed.
Array is optional. Specifies the name of the array that stores the variable. This parameter indicates that the variable is stored in the array.

Tips and comments

Note: If the array parameter is not set, the variables set by this function will overwrite the variables with the same name.

Note: The magic_quotes_gpc setting in php. ini affects the output of this function. If enabled, the variable will be converted by addslashes () before parse_str () resolution.

Example

Example 1

The code is as follows:


<? Php
Parse_str ("id = 23 & name = John % 20 Adams ");
Echo $ id ."
";
Echo $ name;
?>


Output:

The code is as follows:


23
John Adams


Example 2

The code is as follows:


<? Php
Parse_str ("id = 23 & name = John % 20 Adams", $ myArray );
Print_r ($ myArray );
?>


Output:

The code is as follows:


Array
(
[Id] => 23
[Name] => John Adams
)


Php splits the specified character string into an array

$ Str = "1 | 2 | 3 | 4 | 5 | ";
$ Var = explode ("|", $ str );
Print_r ($ var );
?>
$ Var = explode ("|", $ str );
Split $ str by |
Php also splits the specified character string into arrays.
Str_split (string, length) parameter description
String is required. Specifies the string to be split.
Length is optional. Specifies the length of each array element. The default value is 1.
The json_decode () function can also split strings into arrays (the second parameter is true)

What is the php function used to split strings?

This function is substr example: echo substr ('abcdef123', 1, 3); // output bcd

If you split the string into a single character, use split ('ABCDE') to return an array.
If your string has a fixed delimiter, you can use explode ('|', 'a | B | cd | e'). The return value is also an array.

The code is as follows: $ str = "1second = 2 fails = msg = all sent successfully. "; P...

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.