Преобразование строки в двумерный массив и перемешивание - PullRequest
0 голосов
/ 28 июня 2018

Я использую Wordpress и Расширенные пользовательские поля. Моя цель - взять содержимое из поля и преобразовать его в двумерный массив, затем перемешать его. Контент представляет собой серию коротких кодов, которые выглядят как [contentcards url = "..." options = "..."]

function shuffle_content_cards($content) {
  // $content = get_the_content();
  $short_codes = array();
  $the_short_code = '';
  $content = str_replace('<p>', '', $content);
  $content = str_replace('</p>', '', $content);
  if (strpos($content, ']') !== false ) {
  for ($i = 0; $i <= strlen($content); $i++) {
    if ($content[$i] == '[') {
      $start = $i;
    }
    else if ($content[$i] == ']') {
      $end = $i;
      $the_short_code = substr($content, $start, $end+1);
      $content = str_replace($the_short_code, ' ', $content );
      array_push($short_codes, $the_short_code);
// echo $the_short_code;
    }
  }
}
  shuffle($short_codes);
  $content = implode(' ', $short_codes);
  // echo $content;
  return $content;
}
add_filter('acf_the_content', 'shuffle_content_cards', 0);

У меня это работает, так как перетасовывает массив и выводит его, единственная проблема у меня заключается в том, что последний шорткод не отображается. В содержании 7 коротких кодов, и последний не где найти.

Вот результат var_dump ($ short_codes);

array(3) { [0]=> string(169) "[contentcards url="https://www.gotimelinr.com/" custom_image="https://creativesfeed.com/wp-content/uploads/2018/06/timelinr-project-manager.jpg" custom_title="Timelinr"]" [1]=> string(549) " [contentcards url="https://ora.pm" custom_image="https://creativesfeed.com/wp-content/uploads/2018/06/ora-project-management-software.png" custom_title="Ora"] [contentcards url="https://monday.com/" custom_image="https://creativesfeed.com/wp-content/uploads/2018/06/monday-project-management.png" custom_title="Monday" custom_description="monday.com is a tool that simplify the way teams work together - Manage workload, track projects, move work forward, communicate with people - Adopt a management tool that people actually love to use."]" [2]=> string(703) " [contentcards url="https://www.outplanr.com/" custom_image="https://www.outplanr.com/img/screenshots/1.jpg" custom_title="Outplanr"] [contentcards url="https://hitask.com/" custom_image="https://creativesfeed.com/wp-content/uploads/2018/06/hitask-project-manager-software.jpg" custom_title="Hitask"] [contentcards url="https://freedcamp.com/" custom_image="https://creativesfeed.com/wp-content/uploads/2018/06/freedcamp-project-manager.jpg" custom_title="Freedcamp" custom_description="Our mission is to empower people to achieve great things together​. The notion of simple collaboration has been lost and split into hundreds of fragments with countless tools trying to solve each piece."]" }
...