[Solution] generate an array of strings and output a solution based on custom rules.

Source: Internet
Author: User
[Solution] String generation array output string generation array output according to custom rules output according to the function split_array (& quot; Baidu # Baidu | # & quot;, & quot; & lt; ahref = '{1}' [solving] String generation array output according to custom rules
String generation array output according to custom rules
According to the function split_array ("Baidu # http://www.baidu.com/?nbaidu =http://www.baidu.com/#n", "\ n |#"," {0 }");
Output required
Baidu
Baidu

How to write this function?

------ Solution --------------------
Write in this way
PHP code
Split_array ("Baidu # http://www.baidu.com/?nbaidu=http://www.baidu.com/#n", "\ n | #", "{0}"); function split_array ($ str, $ pattern, $ template) {$ cnt = substr_count ($ template, '{'); // count the number of replacement positions in the template $ tmp = preg_split ('/'. $ pattern. '/', $ str,-1, PREG_SPLIT_NO_EMPTY); // cut data by rule $ m = count ($ tmp) % $ cnt; if ($ m) $ tmp = array_slice ($ tmp, 0,-$ m); // remove extra data $ tmp = array_chunk ($ tmp, $ cnt ); // group data into $ r = array (); foreach ($ tmp as $ m) {$ t = $ template; foreach ($ m as $ I => $ v) {$ t = str_replace ("{$ I}", $ v, $ t) ;}$ r [] = $ t ;} echo join ('
', $ R );}

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.