【问题标题】:PHP - Split an array by Upper and Lower Case wordsPHP - 按大小写单词拆分数组
【发布时间】:2021-03-12 01:42:05
【问题描述】:

我有这个数组

$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


    【解决方案1】:

    每当有变化时,我都会使用分隔符来加入案例。我不知道您还可以传递什么其他类型的数据,所以我将分隔符作为一个变量保留,以便轻松更改。

    $separator = '<br>';
    
    foreach($arr2 as $index => $str) {
      $str = explode(" ", $str);
      $final_string = '';
      $last_case = '';
    
      foreach($str as $word) {
        if ($word === strtoupper($word)) {
          if ($last_case == 'lower') $final_string .= $separator.$word;
          else $final_string .= $word . ' ';
    
          $last_case = 'upper';
        } else {
          if ($last_case == 'upper') $final_string .= $separator.$word;
          else $final_string .= $word . ' ';
    
          $last_case = 'lower';
        }
      }
    
      $arr2[$index] = explode($separator, trim($final_string));
    }
    
    $arr2 = array_reduce($arr2, 'array_merge', []);
    
    print_r($arr2); // array ( 0 => 'SUBTITLE', 1 => 'Test Your Might ', 2 => 'RUNTIME', 3 => '1 hr 41 mins', 4 => 'GENRE', 5 => 'Science-Fiction/Fantasy', 6 => 'SYNOPSIS', 7 => 'his film adaptation of the wildly popular video game comes complete with dazzling special effects and plenty of martial arts action.', 8 => 'STARRING');
    

    【讨论】:

      【解决方案2】:

      我会这样做:

      <?php
      $a = ['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'];
      foreach($a as $v){
        if(preg_match('/(?:[A-Z]{2,}(?:\s+|$))+/', $v, $m)){
          foreach($m as $s){
            if($s === $v){
              $r[] = $s;
            }
            else{
              $r[] = str_replace($s, '', $v); $r[] = $s;
            }
          }
        }
        else{
          $r[] = $v;
        }
      }
      print_r($r);
      ?>
      

      【讨论】:

      • 这个可以处理这种情况吗?闪电和特殊效果。我应该把它包括在问题中
      • @LeonardoEcheverria,我已经更新了我的答案。
      【解决方案3】:

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

      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 键/值对,则使用数组后缀会更容易)

      【讨论】:

      • 我没有看到你在哪里定义小写
      • 你说得对——编辑为实例化 $lowercase。顺便说一句,看看您在 StackSlave 的回答中的评论,您可以简单地将 $uppercase 逻辑更改为与 $lowercase 逻辑相同以适应闪电和特殊效果
      猜你喜欢
      • 1970-01-01
      • 2015-08-30
      • 2018-09-07
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 2018-12-28
      相关资源
      最近更新 更多