使用PHP正则表达式将文本文件分割成数组。

问题描述 投票:0回答:1

我有一些字符串中的变更日志文本,我想把每个变更日志条目分割成一个数组。

下面是一个变量中的变更日志文本的例子------。$changelog_txt:

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.

所以,输出的结果将是一个类似于这样的数组。

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.
)

谁能帮忙或提供任何建议?

php arrays regex preg-split
1个回答
1
投票

你可以使用 preg_split() 来制作一个数组。

<?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.org1mpZH

Regex 解释:

/^\s*$/m

^ 表示行首的位置

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

* 量子化器 - 在零和无限次之间匹配,尽可能多的次数,根据需要进行回馈(贪婪)

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

m 修改器:多线。原因 ^$ 以匹配每行的开头(而不仅仅是字符串的开头)。


0
投票

你可以使用 preg_replace_callback() 插入你自己的定界符,然后再使用 explode() 。

<?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
投票
<?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);

结果。

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.


        )

)
© www.soinside.com 2019 - 2024. All rights reserved.