Php uses regular expressions to filter the format specified in the string and define it as an array.

Source: Internet
Author: User
Simple problem: php uses regular expressions to filter the format specified in the string and defines it as an array format: [English letters, numbers, {numbers}] for example: $ str = "this is a [pic, {0}] image, which is a description of [desc, {2}] "; replace" [pic, {0}] "and" [desc, {2}] "is defined as an array after being filtered out using regular expressions, and then the brackets are filtered out to become a simple question of" pic, {0, php uses regular expressions to filter the specified format in a string and defines it as an array.
Format: [English and numbers, {numbers}]
For example: $ str = "this is a [pic, {0}] image, and this is a description of [desc, {2}]";

Filter "[pic, {0}]" and "[desc, {2}]" in the above string using regular expressions and define them as arrays. then, filter out the brackets, to "pic, {0}" and "desc, {2 }".

The final result is:
$ ArrStr = array ();
$ ArrStr ["[pic, {0}]"] = pic, {0 };
$ ArrStr ["[desc, {2}]"] = desc, {2 };

What I want is to take out the specified string format and define it as an array, and then assign a value to the string in the format, "$ arrStr [] = pic, {0 }; "or" $ arrStr [0] = desc, {2}; "is not the expected result.

The names of the objects under the array are to be named based on the retrieved format. the values are filtered out using regular expressions.
------ Solution --------------------
$ Str = "this is a [pic, {0}] Image. this is a description of [desc, {2}]";
$ ArrStr = array ();
Preg_replace ('/\ [(. + ?) \]/E', '$ arrStr ["$0"] = "$1"', $ str );
Print_r ($ arrStr );

------ Solution --------------------
$ Demo = '/\ [([a-zA-Z \ d]. + ?), (\ {\ D + \}) \]/e ';

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.