首页 > 解决方案 > PHP - 按大小写单词拆分数组

问题描述

我有这个数组

$arr2 = array("SUBTITLE", "Test Your Might RUNTIME", "1 hr 41 mins GENRE", "Science-Fiction/Fantasy SYNOPSIS", "his film adaptation of the wildly popular video game comes complete with dazzling special effects and plenty of martial arts action. LIGTHNING AND EFFECT")

而且我想将所有大写的单词与小写的句子分开。像这样。

$arr2 = array("SUBTITLE", "Test Your Might", "RUNTIME", "1 hr 41 mins", "GENRE", "Science-Fiction/Fantasy", "SYNOPSIS", "his film adaptation of the wildly popular video game comes complete with dazzling special effects and plenty of martial arts action.", "LIGHTNING AND EFFECT")

我该怎么做?如果有办法用正则表达式做到这一点,那将是首选。谢谢您的帮助。

标签: php

解决方案


请确认它按预期工作,但一种方法是执行以下操作:

function splitter($jumbled) {
  $lowercase = [];
  foreach($jumbled as $element){
    $exploded = explode(" ", $element);
    foreach ($exploded as $word){
      if ($word == strtoupper($word)){
        $uppercase[] = $word;
      } else {
        $lowercase .= $word . ' ';
      }
    }
    $output[] = $uppercase;
    $output[] = rtrim($lowercase,' ');
  }
 return $output;
}

然后只需使用 arr2 调用该函数:splitter($arr2)

(注意:这会按照您的要求返回数组,但是如果您将相应的值匹配为正在返回的数组中的 $uppercase => $lowercase 键/值对,那么使用数组后缀会更容易)


推荐阅读