首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用正则表达式PHP将文本文件拆分为数组

使用正则表达式PHP将文本文件拆分为数组
EN

Stack Overflow用户
提问于 2020-05-15 00:34:36
回答 3查看 109关注 0票数 0

我有一些字符串中的changelog文本,我想将每个changelog条目拆分为一个数组。

以下是变量- $changelog_txt中的changelog文本示例

代码语言:javascript
复制
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

因此,结果输出将是如下所示的数组:

代码语言:javascript
复制
array(
    [0] => version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
    [1] => version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
)

有没有人能帮上忙或提点建议?

EN

回答 3

Stack Overflow用户

发布于 2020-05-15 00:44:29

您可以使用preg_split()创建数组来完成此操作。

代码语言:javascript
复制
<?php
$str = <<<EOD
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
EOD;
$keywords = array_map('trim',preg_split("/^\s*$/m", $str));
print_r($keywords);
?>

工作演示: https://3v4l.org/1mpZH

正则表达式解释:

代码语言:javascript
复制
/^\s*$/m

^断言行开始处的位置

\s*可以匹配任何空格字符(等于[\r\n\t\f\v ])

*量词-在零和无限次之间进行匹配,尽可能多地进行匹配,根据需要进行反馈(贪婪)

$断言行尾位置全局模式标志

m修改器:多行。使^$匹配每行的开始/结束(不仅仅是字符串的开始/结束)

票数 1
EN

Stack Overflow用户

发布于 2020-05-15 01:03:06

您可以使用preg_replace_callback()插入自己的分隔符,然后使用explode()。

代码语言:javascript
复制
<?php
$oldString ='version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.';

$newString = preg_replace_callback('(version [0-9].[0-9].[0-9][0-9]?)', function($matches){return 'myUnlikelyDelimiter'.$matches[0];}, $oldString);

$array = explode('myUnlikelyDelimiter', $newString);

var_dump($array);
?>
票数 0
EN

Stack Overflow用户

发布于 2020-05-21 18:46:25

代码语言:javascript
复制
<?php
$input_lines = <<<EOD
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD         actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD  actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
EOD;
preg_match_all('/(^[\s\S]*(?:\r*\n{2}))/U', $input_lines, $output_array);
print_r($output_array);

结果:

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [0] => version 4.4.6 ( updated 05-08-2020 )
                - Improved logic to keep collapse/expand state consistent for  Add/Clone/Delete/DnD actions in Layers panel.
                - Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
                - Improved the Visual Builder scroll performance.
                - Added vmin and vmax to css allowed units in module settings.


        )

    [1] => Array
        (
            [0] => version 4.4.6 ( updated 05-08-2020 )
                - Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
                - Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
                - Improved the Visual Builder scroll performance.
                - Added vmin and vmax to css allowed units in module settings.


        )

)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61802614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档