[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 );}