php - 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")
我该怎么做?如果有办法用正则表达式做到这一点,那将是首选。谢谢您的帮助。
解决方案
请确认它按预期工作,但一种方法是执行以下操作:
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 键/值对,那么使用数组后缀会更容易)
推荐阅读
- ios - 展开可选 IBOutlet 时出现错误(线程 1:致命错误:展开可选值时意外发现 nil)
- flask - 如何在flask-jwt-extended中的redis中存储jwt令牌?
- module - 如何使用自定义模块在产品视图页面上添加新按钮以打开模块 url
- tinymce - tinyMCE 编辑器或 Jsoup 可以修复 html 标记错误吗?
- python - python结构的新手,无法运行基本代码
- node.js - 尝试 ./bin/www 时,数据存储不是构造器错误
- xml - 将xml转换为cpp的批处理文件
- azure - 如何找出 Azure 虚拟机的参考对象 ID?
- node.js - 无服务器部署中的自签名证书错误
- javascript - 如何使用 Vuex 在 Tabulator 中添加索引?