"Solve" string generates an array to output a workaround based on a custom rule

Source: Internet
Author: User
The "solve" string generates an array based on a custom rule output
String generation arrays are output based on custom rules
According to function Split_array ("Baidu #http://www.baidu.com/\n Baidu #http://www.baidu.com/\n", "\n|#", "{0}");
Required output
Baidu
Baidu

How do you write this function?

------Solution--------------------
Write
PHP code like this
Split_array ("Baidu #http://www.baidu.com/\n Baidu #http://www.baidu.com/\n", "\n|#", "{0}"); function Split_array ($STR, $ Pattern, $template) {  $cnt = Substr_count ($template, ' {');//The number of alternate locations in the statistics 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 Redundant data  $tmp = Array_chunk ($tmp, $cnt);//Group data  $r = Array ();  foreach ($tmp as $m) {    $t = $template;    foreach ($m as $i = + $v) {      $t = Str_replace ("{{$i}}", $v, $t);    }    $r [] = $t;  }  echo Join ('
  • 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.